バブルソート-php,バブル-php
<span>PHP实现的代码先奉上:<br /><br />function</span> bubble_sort(<span>$array</span><span>) {
</span><span>for</span> (<span>$i</span> = 0; <span>$i</span> < <span>count</span>(<span>$array</span>) - 1; <span>$i</span>++) { <span>//</span><span>$i为已经排过序的元素个数</span>
<span>for</span> (<span>$j</span> = 0; <span>$j</span> < <span>count</span>(<span>$array</span>) - 1 - <span>$i</span>; <span>$j</span>++) { <span>//</span><span>$j为需要排序的元素个数,用总长减去$i</span>
<span>if</span> (<span>$array</span>[<span>$j</span>] > <span>$array</span>[<span>$j</span> + 1]) { <span>//</span><span>按升序排序</span>
<span>$temp</span> = <span>$array</span>[<span>$j</span><span>];
</span><span>$array</span>[<span>$j</span>] = <span>$array</span>[<span>$j</span> + 1<span>];
</span><span>$array</span>[<span>$j</span> + 1] = <span>$temp</span><span>;
}
}
}
</span><span>return</span> <span>$array</span><span>;
}<br /><br />$a = array(5, 1, 4, 7);<br /><br />代码执行过程:<br /></span>
リーリー
リーリー
リーリー
http://www.bkjia.com/PHPjc/934724.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/934724.html技術記事 Bubble sort-php、bubble-php PHP 実装コードが最初に提供されます: function bubble_sort( $array ) { for ( $i = 0; $i count ( $array ) - 1; $i ++) { // $i はソートされた要素の数...