Php Bubble Sort Quick Sort

Source: Internet
Author: User
/******
1) Bubble sort: 22 Exchange values, minimum values on the leftmost, as the lightest bubbles at the top.
2) for the whole column number 22 exchange once, the smallest number on the leftmost, each can get a minimum number in the remaining number, "take" out of the number of an ordered interval, the remaining values constitute an unordered interval, and the order interval of each element value is smaller than the disordered interval.
3) Quick sort: datum number, left and right two arrays, recursive call, merge.
4) Insert Sort: The sorting interval is divided into two parts, the left is ordered, the right is unordered, the right interval takes the first element into the left interval, if this element is larger than the rightmost element of the left interval, and remains in the same place, if this element is smaller than the rightmost element on the left, then the rightmost element is inserted in the right position, and the rightmost Repeat the above steps until the previous element is smaller than the element you want to insert, as compared to the previous element.
6) Note the processing of the interval endpoint value, and the first element of the array is labeled 0.
***/


$a
=Array(' 3 ', ' 8 ', ' 1 ', ' 4 ', ' 11 ', ' 7 ');Print_r($a);$len=Count($a);//from small to large for($i= 1;$i<$len;$i++){ for($j=$len-1;$j>=$i;$j--)if($a[$j]<$a[$j-1]){//if it is from large to small, just change the judgment here to if ($b [$j]> $b [$j-1].$x=$a[$j]; $a[$j]=$a[$j-1]; $a[$j-1]=$x;}}Print_r($a);//Another approach from small to large$b=Array(' 4 ', ' 3 ', ' 8 ', ' 9 ', ' 2 ', ' 1 ');$len=Count($b); for($k= 1;$k<$len;$k++){ for($j=$len-1,$i= 0;$i<$len-$k;$i++,$j--)if($b[$j]<$b[$j-1]){//if it is from large to small, just change the judgment here to if ($b [$j]> $b [$j-1].$tmp=$b[$j]; $b[$j]=$b[$j-1]; $b[$j-1]=$tmp;}Print_r($b);Echo"";}//The following implementation is more efficientfunctionMaopao ($arr){ $len=Count($arr); for($i= 1;$i<$len;$i++)//up to do n-1 sort of trip { $flag=false;//Before this sequencing begins, the swap flag should be false for($j=$len-1;$j>=$i;$j--) { if($arr[$j]<$arr[$j-1])//Exchange Records{//if it is from large to small, just change the judgment here to if ($arr [$j]> $arr [$j-1].$x=$arr[$j]; $arr[$j]=$arr[$j-1]; $arr[$j-1]=$x; $flag=true;//Exchange flag is set to True } } if(!$flag)//The order of this tour does not have an interchange, early termination algorithmreturn$arr; }}$shuz=Array(' 2 ', ' 4 ', ' 1 ', ' 8 ', ' 5 ');$BB= Maopao ($shuz);Print_r($BB);//Quick SortfunctionKuaisu ($arr){ $len=Count($arr); if($len<= 1){ return$arr; } $key=$arr[0]; $left _arr=Array(); $right _arr=Array(); for($i= 1;$i<$len;$i++){ if($arr[$i] <=$key){ $left _arr[] =$arr[$i]; }Else{ $right _arr[] =$arr[$i]; } } $left _arr= Kuaisu ($left _arr); $right _arr= Kuaisu ($right _arr); returnArray_merge($left _arr,Array($key),$right _arr);}$arr=Array(23,98,54,2,9,62,34);Print_r(Kuaisu ($arr));

The above describes a quick sort of php bubble sort, including the insertion of sort content, I hope the PHP tutorial interested in a friend helpful.

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