The basic idea of fast sorting: Through a quick sorting, the records to be sorted into two separate parts, some of which are smaller than the key words of the other record, the two parts of the record can be sorted separately, and the whole sequence is ordered.
Point 1: Pivot selection
Usually choose the first record as a pivot, but the better way is "three to take", that is, compare L.r[s].key,l.r[t].key and l.r[(s+t)/2].key, take three of the key words in the record as the pivot, as long as the record and L.r[s] interchange.
Point 2: At the end of a quick order, the position of the Low==high is the last position of the pivot record.
Point 3: The average time complexity for fast sorting is O (NLOGN), the worst is O (n*n), preferably O (Nlogn). In all the same order of magnitude (O (NLOGN) Sorting algorithms, the average performance is the best. However, if the recorded keywords are basically ordered or ordered, the fast sort is degenerate into a bubble sort, and the time complexity is O (n*n).
Point 4: Quick sort keywords are compared and exchanged for jumps, so fast sorting is an unstable sort method.
Point 5: Spatially, a fast sort requires a stack of space to implement recursion. If each sequencing will record the sequence evenly divided into two sub-sequences of length to close, then the maximum depth of the stack is log2n+1 (including the outermost parameter into the stack), but if the pivot position is biased toward one end of the sub-sequence after each sequencing, the worst case is that the maximum depth of the stack is n, so the spatial complexity of the fast sort (LOGN) ~o (n).
The following is a quick-sort C + + implementation code:
#include < Iostream>using namespace Std;int Partition (int arraynum[],int start,int end) {int Pivotkey=arraynum[start];// Use the first record of the child table as the pivot record//loop condition, alternating from both ends of the table to the middle (start<end) {while (Start<end && Arraynum[end]>=pivotkey) end--;arraynum[start]=arraynum[end];//record smaller than pivot record moves to low end while (Start<end && Arraynum[start]<=pivotkey) start++;arraynum[end]=arraynum[start];//the record larger than the pivot record moves to the high-end}arraynum[start]=pivotkey;//pivot record in place return start;//return to pivot position} void QSort (int arraynum[],int start,int end) {//recursive end Condition Start>=endif (start<end) {int pivotloc=partition (arraynum, Start,end);//The table is divided into 2QSort (arraynum,start,pivotloc-1),//To the low sub-table recursive ordering, Pivotloc is the pivot position qsort (arraynum,pivotloc+1,end);// Recursive ordering of Gaozi}}void QuickSort (int arraynum[]) {QSort (arraynum,0,6);} int main () {int arraynum[7]={30,32,9,64,14,17,8}; QuickSort (Arraynum); for (int i=0;i<7;i++) cout<<arraynum[i]<< ""; Cout<<endl;return 0;}
Improved fast-read sorting algorithm:
#include <iostream>using namespace Std;int Partition (int arraynum[],int start,int End,bool &lowflag,bool &highflag) {int pivotkey=arraynum[start];//Use the first record of the child table as the pivot record//loop condition, alternating from both ends of the table to the middle while (Start<end) {while ( Start<end && arraynum[end]>=pivotkey) {end--;//last time do not swap if (start!=end && arraynum[end]> Arraynum[end+1]) {int temp=arraynum[end];arraynum[end]=arraynum[end+1];arraynum[end+1]=temp;highflag=true;}} arraynum[start]=arraynum[end];//the record smaller than the pivot record moves to the low side while (Start<end && arraynum[start]<=pivotkey) {start+ +;//last time do not swap if (Start!=end && Arraynum[start-1]>arraynum[start]) {int temp=arraynum[start-1];arraynum[ Start-1]=arraynum[start];arraynum[start]=temp;lowflag=true;}} arraynum[end]=arraynum[start];//the record larger than the pivot record moves to the high-end}arraynum[start]=pivotkey;//pivot record in place return start;//back to the pivot position}void QSort (int arraynum[],int start,int end) {//recursive end Condition Start>=endif (start<end) {bool Lowflag=false;bool highflag=false;int pivotloc=partition (ArrayNum, Start,end,lowflag,higHflag);//divide the table into 2if (lowflag==true) QSort (arraynum,start,pivotloc-1);//recursive Ordering of low sub-tables, Pivotloc is the pivot position if (highflag==true) QSort (arraynum,pivotloc+1,end);//Gaozi recursively sort}}void QuickSort (int arraynum[]) {QSort (arraynum,0,8);} int main () {int arraynum[9]={5,4,3,2,1,6,7,8,9}; QuickSort (Arraynum); for (int i=0;i<9;i++) cout<<arraynum[i]<< ""; Cout<<endl;return 0;}
Min--Quick sort