Main content:
1. Algorithm idea
2. Fast sorting algorithm
3, Division algorithm partition
4. Quick-Arrange process diagram
5. Complete code
1. Algorithm idea
The quick sort is a sort of division exchange proposed by C.r.a.hoare in 1962. It adopts a strategy of division, which is usually referred to as the Division method (Divide-and-conquermethod).
(1) The basic idea of division and Administration law
The basic idea of divide-and-conquer method is: to decompose the original problem into several sub-problems with smaller size but similar structure to the original problem. Solve these sub-problems recursively, then combine the solutions of these sub-problems into the solution of the original problem.
(2) The basic idea of fast sequencing
Set the current unordered area to be sorted as R[low. High], the basic idea of fast sequencing can be described by using the divide-and-conquer method:
① decomposition divide:
in R[low. High] To select an element as the Datum (Pivot), which divides the current unordered division into left and right two smaller sub-intervals r[low. PIVOTPOS-1) and R[pivotpos+1..high], and make all recorded keywords in the left sub-range less than or equal to the datum record (may be remembered as pivot) Keyword Pivot.key, all the recorded keywords in the right sub-range are greater than or equal to Pivot.key, and the Datum record pivot is in the correct position (PIVOTPOS), it does not need to participate in subsequent sorting.
② solution Divide:
Quick Sort by recursive call to left and right sub-interval r[low ... PIVOTPOS-1] and R[pivotpos+1..high] quick sort.
③ combination Conquer:
since the two recursive calls in the solve step end, their left and right two sub-bands are ordered. For a quick sort, the combination step does not have to do anything and can be seen as an empty operation.
Analysis of Complexity:
The best complexity O (NLOGN), Pivot is median for each partition.
The worst Complexity O (n^2), the array itself is already ordered.
The partition process destroys the relative order of the elements of the array, so the fast row is not a stable sort.
2. Fast sorting algorithm
void quickSort (vector<int> &num,int i,int j) { int left=i; int right=j; if (left< right) { int index=partition (num,left,right); QuickSort (Num,left,index-1); QuickSort (Num,index+1, right);} }
3, Division algorithm partition
First step: (initialize) set two pointers I and J, their initial values are the lower bound and upper bounds of the interval, namely I=low,i=high; Select the first record of the unordered area R[i] (i.e. R[low]) as the Datum record, and save it in the variable pivot;
The second step: to scan the J from high to the left until the 1th keyword is found to be less than Pivot.key's record r[j], the r[j]) moved to the position I referred to, which is equivalent to R[j] and Datum r[i] (that is, pivot) is exchanged, Moves the record with the keyword less than the base keyword Pivot.key to the left of the datum, the equivalent of pivot in the interchange R[j], and then the I pointer scans to the right from the i+1 position until it finds a record with a 1th keyword greater than Pivot.key r[i], R[i] Move to the position I referred to, which is equivalent to exchanging r[i] and datum r[j], so that the key is greater than the base keyword record to the right of the datum, after the Exchange R[i] is the equivalent of holding pivot, and then the pointer J from the position j-1 start to the left to scan, so alternately change the scanning direction, From each side to the middle of each other, until i=j, I is the final position of the Datum pivot, the pivot is placed in this position to complete a division.
intPartition (vector<int> &num,intLeftintRight ) { intkey=Num[left]; intI=Left ; intj=Right ; while(i<j) { while(I<j && Num[j]>=key) j--; if(I<J) num[i++]=Num[j]; while(I<j && Num[i]<=key) i++; if(I<J) num[j--]=Num[i]; } Num[i]=key; returni;}
4. Quick-Arrange process diagram
5. Complete code:
#include <iostream>#include<vector>using namespacestd;intPartition (vector<int> &num,intLeftintRight ) { intkey=Num[left]; intI=Left ; intj=Right ; while(i<j) { while(I<j && Num[j]>=key) j--; if(I<J) num[i++]=Num[j]; while(I<j && Num[i]<=key) i++; if(I<J) num[j--]=Num[i]; } Num[i]=key; returni;}voidQuickSort (vector<int> &num,intIintj) { intleft=i; intright=J; if(left<Right ) { intindex=partition (Num,left,right); QuickSort (Num,left,index-1); QuickSort (Num,index+1, right); }}intMain () {inta[]={2,5,6,3,4,1,7,9,5,4}; intn=sizeof(a)/sizeof(a[0]); Vector<int> num (a,a+N); QuickSort (num,0, N-1); for(intI=0; i<n;i++) cout<<num[i]<<" "; cout<<Endl; return 0;}
(sort) Quick sort quicksort