Reference book: Data structure and algorithm analysis--c language description
Fast sequencing is the fastest known sorting algorithm in practice, with its average time complexity O (NLOGN). Of course, in the worst case, O (n^2), but a little effort can avoid this situation.
Like merge sort, fast sorting is also a recursive algorithm for splitting, which can be simply represented as follows:
The basic algorithm for sorting the array s is made up of the following simple four steps.
1, the array element is at least greater than or equal to 4, otherwise the direct use of the insertion sort completed.
2, using a specific method (three-digit median segmentation method) to take an array of a specific element V, called the hub element.
3. Divide the other elements in the array (that is, s-{v}) into two disjoint subsets. S1={X∈S-{V} | x≤v} and S2={X∈S-{V} | x≥v}.
4, then two subsets of S1 and S2 for the same 1-3 recursive operation.
How to select Pivot element?
Here we introduce the three-digit median segmentation method. Select the three elements in the leftmost, most right, and center positions of the array, compare the sizes of the three elements, and swap the positions of each other on the left, taking the intermediate values as the pivot element.
For example:
For the initial sequence:
Left=8;right=0;center=6.
It is obvious that the pivot element here should be piovt=6; and the sort at this point is:
Then we select two indicators I and J. The elements in addition to the pivot element are compared with the pivot element, starting from both ends of the sequence. At this point the pivot element 6 can be moved to the location of right-1 (because right is already larger than Piovt, left is smaller than PIOVT).
That
If the number indicated by I is smaller than the pivot element, it i++ until a number greater than or equal to the pivot element is encountered.
If the number indicated by J is larger than the pivot element, then J + + is reached until a number less than or equal to the pivot element stops.
If at this point the i<j (the description element is not all finished), then the number of positions I and J are exchanged. Continue the comparison above until i≥j (the description element is all over).
The completion sequence is as follows:
At this point in the exchange position I of the number and pivot element. As shown in the following:
The pivot element has split the original sequence into two subsets:
1, greater than the pivot element: 7,9,8
2, less than the hub element: 0,1,4,2,3,5
Similarly, two subsets can be continued so that the split sort.
Code implementation:
#include <stdio.h>void QuickSort (int *data,int n), void Qsort (int *data,int left,int right), int Median3 (int *data, int left,int right);//3 number median split method to remove pivot element void Swap (int *a,int *b); void insertsort (int *data,int N); int main () {int i=0; int data[8]={34,8,64,51,32,21,9,5}; printf ("Before sort:\n"); for (i=0;i<8;i++) {printf ("%d\t", Data[i]); } QuickSort (data,8); printf ("\nafter sort:\n"); for (i=0;i<8;i++) {printf ("%d\t", Data[i]); } return 0;} void Swap (int *a,int *b) {int tmp; Tmp=*a; *a=*b; *b=tmp;} int Median3 (int *data,int left,int right) {int center= (left+right)/2; Make sure Data[left]<=data[center]<=data[right] if (Data[left]>data[right]) Swap (&data[left],&data[rig HT]); if (Data[left]>data[center]) Swap (&data[left],&data[center]); if (Data[center]>data[right]) Swap (&data[center],&data[right]); Swap (&data[center],&data[right-1]); Return Data[rigHT-1];} void QuickSort (int *data,int n) {Qsort (data,0,n-1);} void Qsort (int *data,int left,int right) {int i,j; int pivot; if (left+3<=right)//guaranteed at least 4 number {pivot=median3 (data,left,right); I=left; j=right-1; for (;;) {while (data[++i]<pivot); while (Data[--j]>pivot); if (i<j) Swap (&data[i],&data[j]); else break; } Swap (&data[i],&data[right-1]); Qsort (data,left,i-1); Qsort (Data,i+1,right); } else Insertsort (data+left,right-left+1);//Call Insert sort}void insertsort (int *data,int N) {int i=0,j=0; int tmp=0; for (i=1;i<n;i++) {tmp=data[i];//The value to be inserted for (j=i;j>0 && data[j-1]>tmp;j--)//In the sorted array from backward to forward, Until you find the insertion position J Data[j]=data[j-1]; data[j]=tmp; }}
Sorting algorithm--Quick sort