Home  >  Article  >  Backend Development  >  php bubble sort method

php bubble sort method

WBOY
WBOYOriginal
2016-09-01 00:20:17850browse

php bubble sort method
Why the last one can’t be ranked

php bubble sort method

Reply content:

php bubble sort method
Why the last one can’t be ranked

php bubble sort method

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>
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