PHP Bubble sort

Source: Internet
Author: User

Contact PHP So long, also used three kinds of sort, bubble sort, quick sort, cask sort, today to learn about bubble sort:

So what is bubble sort, like a bubble in a river, a bubble emerges, and here is a number, and his principle is to repeat the sequence of visits (traversal) to sort, compare the number of adjacent two, move the large number to the right, and then traverse until all the numbers are completed from small to large. Each time compared to the current largest, the second round to compare the remaining number, with two cycles to do, the outer loop control wheel number, the inner layer loop control the elements of comparison:

On the Code

    1. /**
    2. * Bubble Sort
    3. */
    4. $list = Array (6,8,7,2,3,4,1);
    5. echo "before sort";
    6. Print_r ($list);
    7. Function Mao ($arr) {
    8. For ($i =1, $len =count ($arr), $i < $len, + + $i) {//Outer loop array number-1
    9. For ($k =0, $klen = $len-$i; $k < $klen; + + $k) {//inner loop, compare two elements of an array
    10. if ($arr [$k]> $arr [$k +1]) {
    11. $temp = $arr [$k];
    12. $arr [$k] = $arr [$k +1];
    13. $arr [$k +1] = $temp;
    14. }
    15. }
    16. }
    17. return $arr;
    18. }
    19. echo "
      After sorting ";
    20. Print_r (Mao ($list));
Copy Code

In the process of doing bubbles, the idea has been in the minds of others, in the process of Baidu, see another way, feel good also written over:

    1. $list = Array (6,8,7,2,3,4,1);
    2. echo "before sort";
    3. Print_r ($list);
    4. Function Mao ($arr) {
    5. For ($i =0, $len =count ($arr)-1; $i < $len; + + $i) {//outer loop for first-level traversal
    6. Inner loop, plus one on the outer layer, to control the comparison of two elements
    7. for ($k = $i +1; $k <= $len; + + $k) {
    8. if ($arr [$i]> $arr [$k]) {
    9. $temp = $arr [$i];
    10. $arr [$i] = $arr [$k];
    11. $arr [$k] = $temp;
    12. }
    13. }
    14. }
    15. return $arr;
    16. }
    17. echo "
      After sorting ";
    18. Print_r (Mao ($list));
Copy Code

  

In the process of writing, I admire the latter kind of wording, his thinking is very flexible, because the first one is written according to our normal thinking in doing, very straightforward, feel that thinking is very interesting,

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