Quick sort
Its basic idea is to divide the sorted data into two separate parts by a set of sorting, in which all the data in one part are smaller than all the data in the other part. Then, according to this method, the two parts of the data are sorted quickly, and the whole sorting process can be carried out recursively, and finally all the data are sorted. It becomes an ordered sequence.
Algorithmic design
The basic idea of fast sorting is based on divide-and-conquer strategy. Its algorithm idea is as follows:
- Decomposition: First extract an element from the sequence as a reference element. Based on the benchmark element, the problem is decomposed into two subsequences, so that the subsequences smaller than or equal to the benchmark element are on the left side and the subsequences larger than the benchmark element are on the right side.
- Governance: Quick sorting of two subsequences.
- Merge: Merge the two ordered subsequences together to get the original solution.
source code
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> using namespace std; int Partition(int r[],int low,int high) { int i=low; int j=high; int pivot=r[low]; //Datum variable while(i<j) //Start the cycle when the current bound is less than the upper bound. { while(i<j && r[j]>pivot) //At this time, we first traverse from right to left to find a number smaller than the benchmark value. { j--; //Move from right to left. } if(i<j) { swap(r[i++],r[j]); //Find a number smaller than the baseline value, swap the two numbers, and then start moving from left to right. } while(i<j && r[i]<=pivot) { i++; //Move left to right. } if(i<j) { swap(r[i],r[j--]); //Find a number larger than the baseline value, swap two numbers, and start moving from right to left. } } return i; //i, j two pointers meet, returning the intermediate value middle } void QuickSort(int R[],int low,int high) { int middle; if(low<high) { middle=Partition(R,low,high); QuickSort(R,low,middle-1); QuickSort(R,middle+1,high); //Using the recursive method, we can quickly sort the separated two-segment arrays and repeat the above process. } } int main() { int a[1111]; int i; int n; cout << "Please enter the number of numbers to sort:"<< endl; cin >> n; cout<< "Please enter the data to be sorted:"<< endl; for (i=0;i<n;i++) { cin>> a[i]; } cout << endl; QuickSort(a,0,n-1); cout<< "The sequence after sorting is:" << endl; for (i=0;i<n;i++) { cout << a[i] << " "; } cout << endl; return 0; }
Algorithmic optimization
From the above algorithm, we can see that each exchange is exchanged with the base element, in fact, there is no need to do so. Our aim is to divide the original sequence into two sub-sequences bounded by the reference element. The left sub-sequence is less than or equal to the reference element, and the right sub-sequence is larger than the reference element.
We can scan from right to left to find less than or equal to pivot Of R[j],At the same time, scan from left to right, and find greater than pivot Of R[i]. Give Way R[i]and R[j]Exchange, and then alternate. Until the two pointers meet, the base element and the R[i]Exchange is OK.
int OptimizedQuickSort(int r[],int low,int high) { int i=low; int j=high; int pivot=r[low]; while(i<j) { while(i<j && r[j]>pivot) { j--; } while(i<j && r[i]<=pivot) { i--; } //Simultaneously if(i<j) { swap(r[i++],r[j--]); } } if(r[i]>pivot) { swap(r[i-1],r[low]); return i-1; } swap(r[i],r[low]); return i; }