First, the basic concept
Find an element (in theory you can find one) as a datum (pivot) and then partition the array so that the values of the elements on the left of the datum are not larger than the datum values, the element values to the right of the datum are not less than the datum values, so that the elements of the datum are adjusted to the correct position after sorting. Recursive quick sorting, and other n-1 elements are also adjusted to the correct position after sorting. Finally, each element is in the correct position after sorting, and the sort is complete. So the core algorithm of the fast sorting algorithm is the partitioning operation, that is, how to adjust the position of the datum and adjust the final position of the return datum to divide and conquer the recursion.
Second, select the reference element 1, fixed reference element
If the input sequence is random, the processing time is acceptable. If the array is already in order, the segmentation at this point is a very bad one. Because each partition can only be ordered to reduce the sequence of one, at this time, the worst case, the rapid sorting into a bubble sort, the time complexity of θ (n^2). Moreover, it is quite common for the input data to be ordered or partially ordered. Therefore, it is very bad to use the first element as a datum element, and you should abandon the idea immediately.
2. Random Reference element
This is a relatively safe strategy. Because the position of the datum element is random, the resulting segmentation will not always result in inferior segmentation. When the entire array number is equal, the worst case is still, and the time complexity is O (n^2). In fact, the probability of a stochastic quick ordering to get a theoretical worst case is only 1/(2^n). So randomization fast sequencing can achieve the desired time complexity of O (Nxlog (n)) for the vast majority of input data.
3, three to take in
The best division is to divide the sequence to be sorted into equal-length subsequence, the best state we can use the middle value of the sequence, that is, the number of N/2. However, this is difficult to figure out, and will significantly slow down the speed of sorting. The median estimate can be obtained by randomly selecting three elements and using their median value as the reference element. In fact, randomness does not help much, so the general practice is to use the median of the three elements on the left, right, and center positions as the base element.
Three, partition algorithm
The partition algorithm is the core of fast sorting, and you can learn this algorithm before you learn the fast queue. The following code first:
Public intPartitionint[] num,intLeftintRight ) { if(num==NULL|| num.length<=0 | | left<0 | | right>=num.length) { return0; } intprio=num[left+ (Right-left)/2];//gets the subscript for the middle element of an array while(Left<=right) {//alternating from both ends to the middle scan while(num[left]<Prio) left++; while(num[right]>prio) right--; if(left<=Right ) {swap (num,left,right); //Finally, the base number is returned to the positionleft++; Right--; } } returnLeft ; }
The idea of this method is to find a pivot element (the method is to find the first element, in fact, great article but here to simplify the description), and then from both sides of the array (from where to where the number of incoming parameters) generated two pointers left and right, Each time the left element is found to be greater than the pivot element, I stops, the element on the right is smaller than the hub element J, and the position of the two number is exchanged. Until two pointers left,right meet. Then insert the pivot element into the left position, which is where it should be.
The final result is that the [left,right] portion of the array is rendered with 2 parts, the final position of the pivot element is less than or equal to the pivot element at the left, and the right is greater than or equal to the pivot element. The pivot element is inserted in an absolutely correct position.
Four, sorting algorithm implementation
Packagesort;/*** Quick Sort * Quick sort uses a divide-and-conquer strategy. is to take a reference number in an array, put the small number on the left side of the datum, and the large number on the right side of the datum. * The base left and right are the new sequences respectively. In the new sequence, take a reference number, small left, big on the right. * The recursion used in this. We need three parameters, one is an array, the other two are the bounds of the sequence *@authorHJS*/ Public classquicksort{voidSortintNum[],intLeftintRight ) { if(left<Right ) { intIndex=partition (Num,left,right);//figure out the pivot valueSort (num,left,index-1);//recursive ordering of low sub-tablesSort (num,index+1,right);//recursive ordering of Gaozi } } /*** When calling partition (Num,left,right), divide the num[] and return the position of the datum record *@paramnum *@paramLeft *@paramRight *@return */ Public intPartitionint[] num,intLeftintRight ) { if(num==NULL|| num.length<=0 | | left<0 | | right>=num.length) { return0; } intprio=num[left+ (Right-left)/2];//gets the subscript for the middle element of an array while(Left<=right) {//alternating from both ends to the middle scan while(num[left]<Prio) left++; while(num[right]>prio) right--; if(left<=Right ) {swap (num,left,right); //Finally, the base number is returned to the positionleft++; Right--; } } returnLeft ; } Public voidSwapint[] num,intLeftintRight ) { inttemp =Num[left]; Num[left]=Num[right]; Num[right]=temp; } Public Static voidMain (String args[]) {int[] num={7,3,5,1,2,8,9,2,6}; NewQuickSort (). Sort (num,0,num.length-1); for(intn:num) {System.out.print (n+" "); } }}
Java Quick Sort Parsing