Quick Sort by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.
algorithm processing process (refer to sitting on the toilet to see algorithm: Quick Sort):
Code:
Public classQuickSort { Public Static voidSortint[] arr,intLowintHigh ) { intL =Low ; inth =High ; intTMP = 0; if(l <= h) {//Scan Low index lower than high markTMP =Arr[l]; while(l! = h) {//alternately scan from both sides until L=h while(H>l && arr[h]>tmp) h--; //scan from right to left, find the first number smaller than the baseline data, and replaceARR[L] =Arr[h]; while(l; //scan from left to right to find the first number larger than the base data, and replaceARR[H] =Arr[l]; } Arr[h]=tmp; Sort (arr, low, L-1);//sort data to the left of a datum elementSort (arr, h+1, high);//sort data to the right of a datum element } } Public Static voidMain (string[] args) {int[] arr = {10, 2, 1, 7, 12, 23, 32, 4, 6, 98}; Sort (arr,0, Arr.length-1 ); for(intI:arr) {System.out.println (i); } }}
Time complexity: When the datum data is selected as the maximum or minimum value, the time complexity is O (n^2) and the time complexity is O (nlog2n) If the selected datum data is an intermediate number
Space complexity: O (LOG2N)
Stability: Non-stable sequencing
Quick-row applications
Sort 0-1 Strings (0 and 1 only), you can swap any two positions and ask for the minimum number of exchanges?
Using the fast row, the left 0 and the right 1 can be used without the tube, only the number of times needed to exchange data.
Pseudo code:
000...10100..1111
int count = 0;
for (int i=0,j=len-1; i<j; ++i,--j) {
for (; (i<j) && (a[i]==0); ++i);
for (; (i<j) && (a[j]==1); --J);
if (i<j) ++count;
}
Quick sort (quick-row)