Bubble sort with PHP (small to Large)

Source: Internet
Author: User
    1. array element values from small to large sort
    2. $arr =array (1,42,0,3,15,7,19,26);
    3. Define an intermediate variable
    4. $temp = 0;
    5. Number of outer loops
    6. for ($i =0; $i<>
    7. Two array element values adjacent to the right of the inner layer are compared
    8. for ($j =0; $j<>
    9. When the last array element value is greater than the previous array's original value
    10. if ($arr [$j]> $arr [$j +1]) {
    11. Array element Exchange
    12. $temp = $arr [$j];
    13. $arr [$j]= $arr [$j +1];
    14. $arr [$j +1]= $temp;
    15. }
    16. }
    17. The number of outer array loops is---count ($arr)-1
    18. -1 reason (the array element value is compared when two comparisons are
    19. 3 array elements compared 2 times
    20. echo "This is the first". ($i + 1). " Results of the second comparison ";
    21. echo "
      ";
    22. Print_r ($arr);
    23. echo "
      ";
    24. }
    25. echo " ";
    26. echo "
      ";
    27. Print_r ($arr);
    28. echo "
      ";
    29. ?>
Copy Code
Php
  • Related Article

    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.