Basic idea of quick sort: take any element of the sequence to be sorted as the central element (the first, last, or any one in the middle). It is customary to call it pivot key, that is, pivot element. Place all smaller than the pivot element on its left and all larger than it on its right to form the left and right sub tables, and then sort the left and right sub tables according to the previous algorithm (recursion) until there is only one element left in each sub table.
For example:
arr is [39, 28, 55, 87, 66, 3, 17, 39 *] to distinguish two identical elements, add * to the last one.
The initial state is as follows:
Define a pivot element pivotkey, which is initialized to the value of the first element, namely 39;
The variable of the element on the left of the query is left, i.e. low, and the initial value is the index of the first element, 0;
The variable of the element on the right of the query is right, that is, high, and the initial value is the index of the first element, 7.
As shown below:
Demonstrate the first round of sorting
Start from the right and find one smaller than the pivot element from the right. If right is not found, that is, high is always reduced by 1;
Then assign the current left, i.e. the element where low is located, to this value;
Here, right means that the element referred to by high is not empty, but is set to empty for good demonstration (the same below);
Then start from the left to find an element larger than the pivot element pivotkey; If left is not found, it always increases by 1;
Set the element referred to by the current right to this value;
Then find one smaller than the pivot element from the right. If right is not found, it will always subtract 1;
Set the element indicated by the current left to this value;
Then start from the left to find an element larger than the pivot element pivotkey; If left is not found, it always increases by 1;
Set the element referred to by the current right to this value;
Then find one smaller than the pivot element from the right. If right is not found, it will always subtract 1;
At this time, left and right meet and assign the pivot element to the current position.
Dynamic process of the first round of sorting:
Then divide the array into two parts [17,28,3] and [66,87,55,39 *], and then perform the above steps on these two parts again and again until only one element is left.
Whole sorting process
The complete code is as follows:
#include<bits/stdc++. h> / / universal head using namespace std; const int Max=100; //Definition of sequence table typedef struct { int key; }ordNode; typedef struct { ordNode a[Max]; int len; }Sqlist; //Sort the sub table r[low..high] in the order table L once and return to the pivot position int Partition(Sqlist &L,int low,int high) { L.a[0]=L.a[low]; //Use the first record of the sub table as the pivot record int pivotkey=L.a[low].key; //The pivot record keyword is recorded in the pivotkey while(low<high) { while(low<high&&L.a[high].key>=pivotkey) high--; L.a[low]=L.a[high]; //Move records smaller than pivot records to the low end while(low<high&&L.a[low].key<=pivotkey) low++; L.a[high]=L.a[low]; //Move records larger than pivot records to the high end } L.a[low]=L.a[0];//Pivot record in place return low; //Return to pivot position } //Call the pre initial value: low=1, high=L.length void QSort(Sqlist &L,int low,int high) { int pivotloc; if(low<high) { pivotloc=Partition(L,low,high); QSort(L,low,pivotloc-1); //Recursive call QSort(L,pivotloc+1,high); } } //Quick sort order table L void QuickSort(Sqlist &L) { QSort(L,1,L.len); } int main(){ Sqlist L; printf("Please enter the number of to be sorted:"); scanf("%d",&L.len); printf("Please enter the number to be sorted:"); for(int i=1;i<=L.len;i++) scanf("%d",&L.a[i]); int pivotkey=L.a[1].key; QSort(L , L.a[1].key , L.a[L.len].key); QuickSort(L); printf("Please output the ordered sequence:"); for(int i=1;i<=L.len;i++) printf("%d ",L.a[i]); return 0; }
The test cases are as follows:
Please enter the number of to be sorted:8 Please enter the number to be sorted:39 28 55 87 66 3 17 39 Please output the ordered sequence:3 17 28 39 39 55 66 87
Picture explanation reference Detailed quick sorting algorithm - code essays - blog Garden
I hope I can help you^_^