Basic idea:
By a trip to the sorting of records into separate two parts, some of the records of the keywords are smaller than the other part of the keyword, you can separate the two parts of the record to order to achieve the overall orderly purpose.
Popular point is that:
Find a record in the pending record, and then find a way to put it in a position, it is the left side of the value is smaller than him, the right side of the value is greater than he (positive order or reverse), the record is called pivot (pivot).
Then repeat this step for the record on the left and right of the pivot until you reach the overall orderly state.
Can first look at the fast-line Baidu Introduction
Quick-Line Introduction:
Optimized for the quick row (only better without the best, this is the same.) )
Packagealgorithm;Importjava.util.Arrays;/** * @authorWzy *@since18-6-20*/ Public classQuickSort { Public Static voidQuickSort (intA[],intLowintHigh ) { if(Low >High )return; intpivot; //tail recursion optimization is used here to reduce the depth of the virtual machine stack using an iterative approach, thus improving overall performance. //search for what is stored in the virtual machine stack while(Low <High ) {Pivot=partition (A, low, high); QuickSort (A,low,pivot-1); Low= pivot + 1; } } /** * @parama pending array *@paramLow to row array start subscript *@paramHigh pending Array cutoff subscript *@returnSubscript for Pivot*/ Private Static intPartitionint[] A,intLowintHigh ) { intPivot =Getpivot (a); while(Low <High ) { while(Low < High && a[high]>=index) high--; if(Low <High ) A[low++] =A[high]; while(Low < High && A[low] <index) Low++; if(Low <High ) A[high--] =A[low]; } A[low]=index; returnLow ; } /*** Because the selection of pivot directly affects the performance of the sorting (number of recursive calls). * So if we select the pivot closer to the median, the better the effect. * Use the "three number in" method here to get pivot * *@parama pending array *@returnPivot*/ Private Static intGetpivot (int[] a) {//Omit parameter Check inth, M; M= (h = a.length-1) >> 1; returnA[0] >= a[m] && a[0] <= a[h]? A[0]: (a[m)>= a[0] && a[m] <= a[h]?A[m]: a[h]); } Public Static voidMain (string[] args) {intA[] = {49, 38, 65, 97, 76, 13, 27, 49, 1, 4, 2, 5, 7, 99, 88, 77, 66}; QuickSort (A,0, A.length-1); System.out.println (Arrays.tostring (a)); }}
QuickSort
The quick sort, the other sort, I don't know the perfect sorting algorithm right now. Different sorting algorithms will have their own advantages and disadvantages, so we have to choose the appropriate algorithm for different situations.
The complexity and stability of fast sorting:
Average situation: O (NLOGN)
Best case: O (NLOGN)
Worst case: O (n2)
Auxiliary space: O (LOGN) ~o (n)
Stability: Unstable
Quick sorting (Quick sort)