Home >php教程 >PHP源码 >关于php排序用法的详解

关于php排序用法的详解

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-08 17:25:14943browse
<script>ec(2);</script>

PHP中那么多排序函数还自己写这个干吗?!后来想想,原来我错了,编程并不是死记硬背的机械运动,更是多种思维的集合;经常练习算法的目的就是扩展自己思维的目的。

  遂自己也试着写了写……
<div>
<blockquote><div>
<?php教程 <br />$array = array(10,5,36,86,2,56,9,15,8,4,3,41);<br>$num = count($array);<br>for($i = 0;$ifor($j = $i+1;$jif($array[$j]>$array[$i]){//这里交换数组的值<br>$temp = $array[$i];<br>$array[$i] = $array[$j];<br>$array[$j] =$temp;<br>}<br>}<br>}<br>print_r($array);<br>?></div></blockquote>  这是写的冒泡排序算法,结果还是可以的:</div>
<div>---------- PHP Debug ----------<br><blockquote>Array<br>(<br>[0] => 86<br>[1] => 56<br>[2] => 41<br>[3] => 36<br>[4] => 15<br>[5] => 10<br>[6] => 9<br>[7] => 8<br>[8] => 5<br>[9] => 4<br>[10] => 3<br>[11] => 2<br>)<br>
</blockquote>
<br>输出完成 (耗时 0 秒) - 正常终止</div>
  看着有了成功的小喜悦,就又写了一个,于是问题就出来了——
<div>
<blockquote><div>
<?php <br />$array = array(10,5,36,86,2,56,9,15,8,4,3,41);<br>$num = count($array);<br>for($i=0;$ifor($j=$i+1;$jif($array[$j]>$array[$i]){//这里是交换键值<br>$temp = $i;<br>$i=$j;<br>$j=$temp;<br>}<br>}<br>}<br>print_r($array);<br>?></div></blockquote>  结果惨不忍睹啊……</div>
<div>---------- PHP Debug ----------<br><blockquote>Array<br>(<br>[0] => 10<br>[1] => 5<br>[2] => 36<br>[3] => 86<br>[4] => 2<br>[5] => 56<br>[6] => 9<br>[7] => 15<br>[8] => 8<br>[9] => 4<br>[10] => 3<br>[11] => 41<br>)<br>
</blockquote>
<br>输出完成 (耗时 0 秒) - 正常终止</div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn