Help me figure out what's wrong with this bubble sort.
HUNT
HUNT 2017-08-22 21:22:47
0
2
1020

<?php
$arr=array(10,3,1,8);

function bubble(&$arr){
    $total=count($arr);
    for($i=0;$i<$total - 1;$i++){
        for($j=0;$total - 1 - $i;$j++){
            if($arr[$j] > $arr[$j + 1]){
                $temp=$arr[$j];
                $arr[$j]=$arr[$j+1];
                $arr[$j+1]=$temp;
            }
        }
        echo '<br>';
    }
}
bubble($arr);
echo '<br>';
print_r($arr);
?>

HUNT
HUNT

reply all(2)
风豆丁

The intermediate conditional statement of the second for loop should be $j < $total - 1 - $i

Ty80

I have not studied the PHP sorting algorithm. You can use the PHP sorting functions sort, asort, rsort, krsort, and ksort to sort arrays, which is simpler.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template