The quick sort of dig pit method and Prev, cur method, we in the last blog of the 6th sort in the very detailed, http://10740184.blog.51cto.com/10730184/1774508 "data structure" commonly used sorting algorithm (including: select Sort, Heap sort, bubble sort, select sort, quick sort, merge sort)
If you are interested, believe that you are smart, you will know the idea of a quick sort in seconds.
Below, we will quickly sort the optimizations:
1, Three to optimize the fast sorting
Optimization Reason:
The quick sort of wiping almost every time the sequence is divided into half, the time complexity is O (N*LGN).
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/80/5C/wKiom1c-ku6jIeVzAAAhXifwo6A279.png "title=" 11.png "alt=" Wkiom1c-ku6jievzaaahxifwo6a279.png "/>
We think that the time complexity of the quick Sort is O (N*LGN), which is very efficient in sequence chaos. However, when the sequence is orderly or close to order, the efficiency is not so high.
If a sequence is like this:
{10,5,1,4,5,9,6,1}
We aim at the above sequence, if we want to rank in ascending order, we need to find a number to meet the excavation method inside of the data conditions, or the Prev, cur in the exchange of data conditions, may have been the sequence from the beginning, find the end or close to find or not found, this time equivalent to the efficiency of the O (n ^2).
Optimization method:
Therefore, we think of the three-digit idea. That is, the sequence of three positions leftmost left, rightmost right, middle mid, three numbers out of the middle size, use this number to do key.
The code in the three-digit fetch is as follows:
Int mid (int* a, int left, int right) { int mid = left - (left - right) / 2; if (a[left] < a[right]) { if (A[left] > a[mid]) { return a[left]; } else { if (a[right] < a[mid]) { return a[right]; } else { return a[mid]; } } } else { if (A[right] > a[mid]) { return a[right]; } else { if (a[left] < a[mid]) { return a[left]; } else { return a[mid]; } } }}
2. Non-recursive implementations
Optimization Reason:
When a sequence is small, the sequence is processed in two halves at a time, and the sequence of two halves is disposed recursively.
However, when a sequence of 200,000 or 300,000 is to be ordered, each recursive, there is no doubt that each call to the function to build the stack frame will be very expensive, or even drain the system space.
Optimization method: Stack stack simulation is implemented to implement stack frame, each stack stack---"is recursive.
The recursive code is as follows:
Int partsort (int* a, int left, int right) { assert (a); int cur = left; int prev = cur - 1; int key = mid (a,left,right); swap (Key, a[right]); while (cur < right) { if (A[cur] <= a[right]) { swap (a[ ++prev], a[cur]); } cur++; } swap (A[++prev], a[right]); return prev;} Void quicksort (int* a, int left, int right) {&NBSP;&NBsp; int prev = partsort (a,left, right); if (prev - 1 > left) { QuickSort (a, left, prev - 1); } if ( Prev + 1 < right) { quicksort (a, prev + 1, right); }}
The non-recursive code is as follows ( recommended ):
Prev, cur method, can also be used to dig pits and other methods Int partsort (Int* a, int left, int right) { assert (a); int cur = left; int Prev = cur - 1; int key = mid (a,left,right); swap (Key, a[right]);//Exchange The data obtained in the three-digit collection with A[right]. while (cur < right) { if (A[cur] <= a[right]) { swap (A[++prev], a[cur]); } cur++; } swap (A[++prev], a[right]); return prev;} Void quicksort_nonr (INT*&NBSp;a, int left, int right) { stack<int> s; //to the left and right interval pressure into the stack, or at this time can also define a structure, there are left and right intervals, once the left and right zones are pressed into the s.push (leave); s.push (right); while (!s.empty ()) { //[left,right] int Curright = s.top ();//pressure stack first pressure is the left interval, advanced out, take the data first to take the right interval s.pop (); int curleft = s.top (); s.pop (); int prev = Partsort (a, curleft, curright);//A quick sort of this interval if (Prev - 1 > curleft) { s.push (curleft);//The left-hand sequence of the press-in new points s.push (prev - 1); } if (prev + 1 < curright) { S.push (prev + 1);//The Right section of the press-in of new points S.push (curright); } }}
This article is from "Han Jing's Blog", please make sure to keep this source http://10740184.blog.51cto.com/10730184/1775357
"Data structure" large amount of data (200,000) for fast sequencing of recursive and non-recursive algorithms, three-digit thinking