Bubble sorting is relatively straightforward. As many companies interview pen questions often appear, require handwritten this sorting algorithm. Double-layer cycle, continuous and the subsequent comparison, if greater than the subsequent, the order can be reversed.
Demo effect
The code is as follows:
<?PHPfunctionBubblesort ($array=Array()){ $length=Count($array);//Get Array length for($i= 0;$i<$length;$i++) { #Loop Array for($j=$i+1;$j<$length;$j++) { #itself compared to the people behind itself if($array[$i]>$array[$j]) { //The order is not reversed to implement bubbling//two number exchange value $tmp=$array[$i];//give the first value to a temporary variable $array[$i]=$array[$j];//give the value of J to I $array[$j]=$tmp;//give the TMP to J . } } } return $array;//returns an array}?>
The calling code is as follows:
<? PHP $array=array(10,9,8,7,6,5,4,3,2,1); $result=bubblesort ($array); Var_dump ($result);? >
Effect
A sort of bubble sort in PHP