Php Bubble sort Quick Sort, php bubble sort _php Tutorial

Source: Internet
Author: User

Php Bubble sort Quick Sort, php bubble sort


/******
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, remains in place, if this element is smaller than the rightmost element of the left interval, then the rightmost element is inserted to the right, and the Calculator minus one, re-compares the previous element until the previous element is smaller than the element to be inserted, repeating the above steps.
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 algorithm return $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); return Array_merge($left _arr,Array($key),$right _arr);}$arr=Array(23,98,54,2,9,62,34);Print_r(Kuaisu ($arr));

http://www.bkjia.com/PHPjc/1082224.html www.bkjia.com true http://www.bkjia.com/PHPjc/1082224.html techarticle php Bubble sort quick Sort, php bubble sort/****** 1) bubble sort: 22 swap values, minimum values on the leftmost, as the lightest bubbles at the top. 2) to the whole column number 22 exchange ...

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