Home >Backend Development >PHP Tutorial >php bubble sort method
Why the last one can’t be ranked
Why the last one can’t be ranked
You should let $i=0 in the first for
================================
I guess you made a mistake and copied $i into $j.
j = 0;
j = 1;
j = 2;
//Oops, I accidentally copied it wrong and only executed it 3 times
exit;
The number of loops is wrong, $j
The second level loop should be -$i, not minus $j
<code>$arr = array(21,7,5,23,2); var_dump($arr); $long = count($arr); for($i=0;$i<$long;$i++){ for($j=0;$j<$long-$i-1;$j++){ if($arr[$j]>$arr[$j+1]){ $x = $arr[$j+1]; $arr[$j+1]=$arr[$j]; $arr[$j]=$x; } } } var_dump($arr);</code>