Fast sequencing is the fastest known sorting algorithm in practice, its average run time is O (nlogn), the algorithm is particularly fast, mainly due to very refined and highly optimized internal loops. its worst-case performance is n^2.
The quick sort consists of the following simple four steps:
1. Returns if the number of elements in S is 0 or 1.
2. Take S in any element V, called the hub element
3. Divide S into two disjoint sets, the previous element is less than v, and the latter element is greater than v.
4. After returning quicksort(S1), add V, plus quicksort( S2);
How do I select pivot elements?
There is a safe way to randomly select a pivot element, but the cost of generating a random number is very expensive, reducing the average elapsed time of the remainder of the algorithm.
Here is a method called the three-digit median segmentation method, the best value of the pivot element is the median of the array, that is, the largest number of N/2, but this is difficult to work out, and seriously slow down the algorithm speed. Then back to the second, we use the left end of the array, the center, the right side of the value of the position to do the pivot element.
The quick-Sort exchange strategy is to exchange the pivot element and the last element before starting, so that the pivot element leaves the array to be sorted, since the position of the pivot element is certain when the array is sorted. I start with the first element, J starts with the second last element, and when I is on J left, we move I to the right, move the element smaller than the pivot element, move J to the right, and move over those elements that are larger than the pivot element. When the i,j stops, I points to an element that is greater than the pivot element, and J points to an element less than the pivot element, so that the effect is ultimately that all elements smaller than the pivot element are to the left of the pivot element, and the element greater than the pivot element is on its right. Thus achieving a sort of willingness.
void QuickSort (int a[],int length) {Qsort (a,0,length-1);} int median3 (int a[],int left,int right)//Get pivot element, use three-digit median method {int center= (left+right)/2;if (A[left]>a[center]) swap (& A[left],&a[center]), if (A[left]>a[right]) swap (&a[left],&a[right]), if (A[center]>a[right]) swap ( &a[center],&a[right]); swap (&a[center],&a[right-1]);//Place the pivot element at the end of the array and return to A[right-1];} void Qsort (int a[],int left,int right) {int i,j;int pivot;if ((left+3) <=right) {pivot=median3 (a,left,right); i=left,j =right-1;for (;;) {while (A[++i]<pivot) {}//Because the first time is a[i] is less than pivot,a[j] is greater than pivot, so with ++iwhile (A[--j]>pivot) {}if (i<j) Swap (&a[ I],&A[J]);//appears equal to the case, swap, evenly allocated to the elsebreak in the sub-array;} Swap (&a[i],&a[right-1]); Qsort (a,left,i-1);//Before the position of I, all elements are smaller than it, after I, all elements are greater than it, so a[i] position does not need to change Qsort (a,i+1,right);} else//when the array is very small, just insert sort, exit recursion, do not need to wait until the array length equals 1, because for the decimal group, the insertion sort is better than the quick sort insertionsort (a+left,right-left+1);// The insert sort routine has been implemented in the previous article}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C-language implementation of sorting algorithm-quick sort