Ec (2); why do so many sort functions write this in PHP ?! Later I thought about it. I was wrong. programming is not just a mechanical movement, but a set of multiple ways of thinking. The purpose of practicing algorithms is to expand my own thinking. I also tried to write & hellip; & lt ;? Php tutorial $ arrayarray (,); $ numcount ($ array); for (script ec (2); script
Why are so many sorting functions in PHP still writing this ?! Later I thought about it. I was wrong. programming is not just a mechanical movement, but a set of multiple ways of thinking. The purpose of practicing algorithms is to expand my own thinking.
I also tried to write ......
$ Array = array );
$ Num = count ($ array );
For ($ I = 0; $ I <$ num-1; $ I ++ ){
For ($ j = $ I + 1; $ j <$ num; $ j ++ ){
If ($ array [$ j]> $ array [$ I]) {// here, the value of the array is exchanged.
$ Temp = $ array [$ I];
$ Array [$ I] = $ array [$ j];
$ Array [$ j] = $ temp;
}
}
}
Print_r ($ array );
?>
This is the Bubble sorting algorithm, and the result is still acceptable:
---------- PHP Debug ----------
Array
(
[0] => 86
[1] => 56
[2] => 41
[3] => 36
[4] => 15
[5] => 10
[6] => 9
[7] => 8
[8] => 5
[9] => 4
[10] => 3
[11] => 2
)
Output completed (0 seconds)-normal termination
With the little joy of success, I wrote another one, and the problem came out --
$ Array = array );
$ Num = count ($ array );
For ($ I = 0; $ I <$ num-1; $ I ++ ){
For ($ j = $ I + 1; $ j <$ num; $ j ++ ){
If ($ array [$ j]> $ array [$ I]) {// here is the exchange key value
$ Temp = $ I;
$ I = $ j;
$ J = $ temp;
}
}
}
Print_r ($ array );
?>
The results are terrible ......
---------- PHP Debug ----------
Array
(
[0] => 10
[1] => 5
[2] => 36
[3] => 86
[4] => 2
[5] => 56
[6] => 9
[7] => 15
[8] => 8
[9] => 4
[10] => 3
[11] => 41
)
Output completed (0 seconds)-normal termination