php algorithm

WBOY
WBOYOriginal
2016-06-23 13:52:12949browse

<?php //php algorithm bubble // 3, 8 , 99, 75, 23, 1, 29, 51, 24, 13, 78.//第一次循环,如果有小元素就往后走,即冒泡,这样循环一次,最小的1就冒泡到最顶部了,即$array[10]=1;//第二次循环,如果有小元素就往后走,及冒泡,这样循环一次,第二小的3就冒泡到倒数第二的位置了,即$array[9]=3;//...以此类推。遍历循环数组的个数-1次,就按照从大到小将数组排序了,//其中可以做些优化,第一次冒泡需要冒泡到最后一个元素,第二次就冒泡到倒数第二个元素就ok了,即count($array)-2,还可以优化的地方这里就不做深入探讨了。function dump($arr){	echo '<pre class="brush:php;toolbar:false">';	print_r($arr);	echo '
';}$array = array(3,8,99,75,23,1,29,51,24,13,78);dump($array);for($i=0;$i
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