Php sorting usage-PHP source code

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.