Sort of bubble sort (optimized)-php

Source: Internet
Author: User

1<?PHP2     //set the request header3     Header("Content-type:text/html;charset=utf-8");4     5     //optimize for bubbling sorting: If an array is passed in or traversed two times6 //It is already lined up and no more sorting is done7 8 ///Examples:9     $arr=Array(1,2,3,6,4,12,1,85,1,3);//WaitSort ArraysTen //$arr = range (1, 10000); One      A     functionMy_bubble_sort (&$arr){ -         Var_dump($arr); -          the         $arr _count=Count($arr); -  -         //Number of comparisons -          for($i= 0;$i<$arr _count-1;$i++){ +              -              for($j= 0;$j<$arr _count-$i-1;$j++){ +                  A                 //move big to the right, move small to the left (from big to small only need the symbol) at                 if($arr[$j] >$arr[$j+1]){ -                     //Swap Operations -                     $temp=$arr[$j]; -                     $arr[$j] =$arr[$j+ 1]; -                     $arr[$j+ 1] =$temp; -                     $flag= 1; in                 } -             } to  +             if(!$flag){ -                 //If $flag=0 indicates that the small for loop has not been exchanged 1 times (without a single comparison, you can say that the array is sorted) the //You don't need to sort. *                  Break; $}Else{Panax Notoginseng                 $flag= 0; -             } the         }     +  A         Var_dump($arr); the     } +  -       $  $Date_default_timezone_set (' PRC '); -      - //echo "Start:". Date (' H:i:s '). ' <br> '; theMy_bubble_sort ($arr); - //echo "End:". Date (' h:i:s ');Wuyi  the?>

Sort by bubble sort (optimization)-php

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.