PHP Bubble sorting algorithm implementation code

Source: Internet
Author: User
    1. !--? php //bubble sort function
    2. //This function uses references to avoid memory consumption
    3. //collation: bbs.it-home.org
    4. functi On &bubble (& $arr) {
    5. $count =count ($arr);
    6. if ($count >1) {
    7. for ($i =0; $i < $count; $i + +) {
    8. for ($j = $count-1; $j >= $i; $j-) {
    9. if ($arr [$j -1]> $arr [$j]) {
    10. $temp = $arr [$j-1];
    11. $arr [$j -1]= $arr [$j];
    12. $arr [$j]= $temp;
    13. }
    14. }
    15. }
    16. }
    17. return $arr;
    18. }
    19. //output array (for easy viewing on Web pages)
    20. function Printarr (& $arr) {
    21. echo "
      ";  
    22. Print_r ($arr);
    23. echo "
    24. ";
    25. }
    26. //Start Test
    27. //assign array random 10 values
    28. for ($i =0; $i <10; $i + +) {
    29. $testAr R[]=rand (10,100);
    30. }
    31. Printarr (Bubble ($TESTARR));
    32. ?>
Copy Code

>>> you may be interested in the article: PHP Practical fast sorting Algorithm for example code PHP implementation of various sorting algorithms summary php bubble sort Small example PHP implementation bubble sort Simple example php two-dimensional array sorting custom function PHP Choice sort of implementation code PHP Implementation code of the bubble sort PHP Insert Sort implementation code PHP implementation Quick Sort function PHP Implementation quick sort function php bubble sort Exchange Sort method php bubble sort (bubble sort) Example PHP implementation bubble sort algorithm code php bubble sort algorithm A case of PHP bubble sort and Quick Sort example php two-dimensional array ordering of PHP multidimensional array

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