The basic idea of the bubble sorting method is that every time a cycle is passed, the maximum exchange is made to the last, and finally it becomes an ordered array from small to large;
Small cases: from small to large, and then from big to small;
<?php$arr1=array (1,-1,10,-9,0,3,7,-20,100), function Sort1 ($arr) {for ($i =0; $i <count ($arr)-1; $i + +) {for ($j = 0; $j <count ($arr)-$i; $j + +) {if ($arr [$j]> $arr [$j +1]) {$temp = $arr [$j]; $arr [$j]= $arr [$j +1]; $arr [$j +1]= $temp; }}}return $arr;} $BRR =sort1 ($arr 1);p rint_r ($BRR); $j =0;for ($i =count ($BRR)-1; $i >=0; $i-) {$CRR [$j]= $brr [$i]; $j + +;} Print_r ($CRR);? >
[[email protected] html]# php sort1.php Array ([0] = -20 [1] = 9 [2] +-1 [3] = 0 [4] => ; 1 [5] = 3 [6] = 7 [7] = [8] = +) Array ([0] = [1] = [2] = 7 [3] = 3 [4] = 1 [5] = 0 [6] +-1 [7] +-9 [8] =-20)
This article is from the "Progress 1% per day" blog, please be sure to keep this source http://jackyan.blog.51cto.com/2589874/1545937
PHP Bubble Sort Method