PHP Common Four sort bubble, select, insert, Quick row

Source: Internet
Author: User
Tags explode

---restore content starts---

1 Bubble sort. "Easy to describe, examples are all in ascending order"
Description: Suppose the array has 10 numbers, from left to right. In turn, if the former is greater than the latter, then 22 is exchanged. Each round will bubble a maximum number out, loop in turn, finish sorting
Process Description :
--first time a[0] and a[1] than if a[0] > a[1] then a[0] and a[1] exchange, then a[1] and a[2] interchange, then to A[8] and a[9] Exchange. The maximum value in a[9] must be a[0-9] after this round.
The second time (at this time a[9] is the maximum) a[0] and a[1] than if a[0] > a[1] then a[0] and a[1] exchange, then a[1] with A[2] interchange, then to A[7] and a[8] Exchange. The maximum value in a[8] must be a[0-8] after this round.
The third time (at this time a[9] with the maximum value, a[8] times Large) a[0] and a[1] than if a[0] > a[1] then a[0] and a[1] exchange, then a[1] and a[2] exchange, then to A[6] and a[7] Exchange. The maximum value in a[7] must be a[0-7] after this round.
--------------so repeatedly, one bubble at a time is the maximum value out.

Bubble Sort code Example
   Public function bubblesort ($arr) {  // bubble Sort
        $len=sizeof($arr);  for($i= 0;$i<$len-1;$i++){//Take the first few bubbles             for($j= 0;$j<$len-$i-1;$j++){//do not take the bubble to compare in turn, find the maximum value                if($arr[$j] >$arr[$j+1]){                    $arr= Swap ($arr,$j,$j+1); }            }        }        return $arr; }    //swaps the first J bits of the array. The following code is slightly     Public functionSwap$arr,$i,$j){//        $temp=$arr[$i]; $arr[$i] =$arr[$j]; $arr[$j] =$temp; return $arr; }

2 Select Sort
briefly , suppose that the array has 10 numbers, from left to right 1 to 9 for each of the numbers to the right, to find the smallest right and replace it.
Process Description :
--The first time a[0] is paired with a[1]-a[9], the minimum value is found, and then the minimum value in a[0] and a[0-9] is replaced. Replace a[0] array minimum (not replaced if 0 is exactly the smallest)
The second time a[0], to be the smallest value, proceed to the next step. A[1] to a[2]-a[9], find the minimum value, and then a[1] and a[1-9] the smallest replacement.
The third time a[0], to be the minimum, a[1] is an array of sub-small values (the first two elements are ordered) a[2] each with a[3]-a[9] match, find the minimum value, and then a[2] and that a[2-9] the minimum value of the replacement.
............... Loop in turn to finish sorting.

Select the Sort code sample
functionSelectsort ($arr){//        $len=sizeof($arr);  for($i= 0;$i<$len-1;$i++){//traverse from left to right, and find the minimum value of the I bit each time.
$minIndex=$i; for($j=$i+1;$j<$len;$j++){//iterate to the right of I constantly find the minimum value. if($arr[$minIndex] >$arr[$j]){ $minIndex=$j; } } //Position Exchange if($i!=$minIndex){//Replace the leftmost and right-most-left lookup with the minimum value. $arr=$this->swap ($arr,$i,$minIndex);; } } return $arr;}


3 Quick Sort (emphasis)
Description:
set a cardinality and then cut left and right on the array.  (the left is smaller than the cardinality, the right is greater than or equal to the cardinality), and then the left array. Finally, the left array cardinality right data is merged

 //Fast Sorting algorithm  Public functionQuickSort ($arr){        $len=sizeof($arr); if($len<=1)return $arr; $base=$arr[0];//cardinality        $l _arr=Array();//left Array        $r _arr=Array();//Right Array         for($i= 1;$i<$len;$i++){            if($arr[$i] <$base){                $l _arr[]=$arr[$i]; }Else{                $r _arr[]=$arr[$i]; }        }        $l _arr= QuickSort ($l _arr); $r _arr= QuickSort ($r _arr); return Array_merge($l _arr,Array($base) ,$r _arr); }

4 Insert sort (included, can skip)
briefly , suppose that the array has 10 numbers, starting from the second of the array, comparing each left to the left is an ordered array, and then to the left to find its own position, a ratio.
be subject to the language ability, write very Rao mouth.

--the first a[1] and a[0] than, if A[1] < a[0] will a[0] data moved to a[1] bit will a[1] the value of the insertion into the a[0] position.

--second a[2] (to insert value) and a[0], a[1] (already ordered) if A[2] (to insert value) > A[1] then a[2] (the value to be inserted) does not move.  If less than a[1] moves the original a[1] value to a[2], a[2] (the value to be inserted) is placed in a[1]. and a[0] than, if greater than a[0] then a[0] value into a[1].

 Public functionInsertsort ($arr){        $len=sizeof($arr);  for($i= 1;$i<$len;$i++){            $temp=$arr[$i];  for($j=$i-1;$j>=0;$j--){//For an ordered data, find the insertion bit in turn                if($temp<$arr[$j]) {//insert individually. $arr[$j+1] =$arr[$j]; $arr[$j] =$temp; }Else{                     Break; }            }        }        return $arr; }

Written in the end: To have a lot of data, there is the concept of magnitude. The complexity of the fast line is O (n log n), and the bubble sort is O (n squared)
Take only 10,000 numbers for example.  The complexity is 10000 * LOG2 10000 is about 13* million, bubbling is 100 million. The difference is about 800 times times the performance.
If it is 100,000 fast rows then 100,000 * log2 100000 is about 1.7 million and bubbling is 10 billion. An instantaneous difference of about 7,000 bits.

And 100,000 of the data, in real scenes, really not much.

If the feeling is not obvious, run the code experience.

/** Performance Debugging. */functionBench_profile ($starttime,$flag= ' '){    $endtime=Explode(‘ ‘,Microtime()); $thistime=$endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]); $thistime=round($thistime, 3); return  $flag." -bench: ".$thistime. "SEC";} $start _time=Explode(‘ ‘,Microtime()); EchoBench_profile ($start _time)." <br/> "; $arr= [];  for($i= 0;$i<10000;$i++){                $arr[]=Rand(1,100000); }            $arr _insert= QuickSort ($arr); EchoBench_profile ($start _time);//1w Run Time-bench:0.053 sec bubbling algorithm. About 45 seconds//10w run out of time-bench:0.77 sec bubbling algorithm about 1.5 hours. 1 million, run time 16 seconds.  Then you need to release the memory limit Ini_set ("Memory_limit",-1). The bubbling algorithm is also used. Come and see the results tomorrow. 

PHP Common Four sort bubble, select, insert, Quick row

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.