Quick Sort
First, quickly sort the steps:
- First select the axis value
- Divides the content to be sorted into two parts, the left is less than or equal to the axis value, the right is greater than the axis value
- Then repeat the above steps to the left and right until the entire sequence is ordered
- Directly on the code here first write the divided code
- One division here is that the first number is the axis value, and we can use the last or the middle one.
#include <iostream>#include<vector>using namespacestd;//an array without a staging unit v1inttemp1[]={ -, -, -, $,98, -, at, the, -, -};vector<int>v1 (Begin (Temp1), End (Temp1));//Print ArrayInlinevoidPRINTV (Constvector<int> &v) { for(auto a:v) cout<<a<<" "; cout<<Endl;}//First Division//Quick Sort First DivisionintPartition (vector<int> &v,intFirstintend) { intI=First ; intj=end; Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<" "; while(i<j) { while(i<j&&v[i]<V[j]) {J--; cout<<"at this time i="<<i<<"at this time j="<<j<<Endl;} if(I<J)//if i<j exchange ij{cout<<"Exchange"<<" "; inttemp=V[j]; V[J]=V[i]; V[i]=temp; I++; Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<" "; } cout<<Endl; while(i<j&&v[i]<=V[j]) {i++; cout<<"at this time i="<<i<<"at this time j="<<j<<Endl;} if(i<j) {cout<<"Exchange"<<" "; inttemp=V[j]; V[J]=V[i]; V[i]=temp; J--; Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<" "; }} printv (v); returni;}//Here we first call a division understanding how to divideintMainintargcChar*argv[]) {Partition (v1,0, V1.size ()-1); return 0;}
Then run the analysis
- First take 59 as the axis value, then because the left is smaller than 59, the right is greater than 59
- 59 and 28 comparison found 59 greater than 28 swap
- Continue traversing to the left after swapping. So i++, traversing to 98 found 59 small, exchanged
- Then the left side must be less than 59, but the right side is not sure, start traversing J
- J=8 found that 13 is less than 59 then swapped again, then the right must be greater than 59, left indeterminate, continue to traverse I
- I=7 59<83 Exchange
- This is i=j not meet i<j conditions jump out of the loop
- At this point 59 is the axis value, the left is less than 59 to the right is greater than
And then there's the complete, recursive, fast-line.
First on the code
#include <iostream>#include<vector>using namespacestd;//an array without a staging unit v1inttemp1[]={ -, -, -, $,98, -, at, the, -, -};vector<int>v1 (Begin (Temp1), End (Temp1));//Print ArrayInlinevoidPRINTV (Constvector<int> &4) { for(auto a:v) cout<<a<<" "; cout<<Endl;}//Quick Sort One DivisionintPartition (vector<int> &v,intFirstintend) { intI=First ; intj=end; Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<" "; while(i<j) { while(i<j&&v[i]<V[j]) {J--; cout<<"at this time i="<<i<<"at this time j="<<j<<Endl;} if(I<J)//if i<j exchange ij{cout<<"Exchange"<<" "; inttemp=V[j]; V[J]=V[i]; V[i]=temp; I++; Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<" "; } cout<<Endl; while(i<j&&v[i]<=V[j]) {i++; cout<<"at this time i="<<i<<"at this time j="<<j<<Endl;} if(i<j) {cout<<"Exchange"<<" "; inttemp=V[j]; V[J]=V[i]; V[i]=temp; J--; Printv (v); cout<<"at this time i="<<i<<"at this time j="<<j<<" "; }} printv (v); cout<<"once part ends"<<Endl; returni;}//Quick LinevoidQuickSort (vector<int> &v,intFirstintend) { if(first<end) { intpivot=Partition (v,first,end); QuickSort (V,first,pivot-1);; QuickSort (V,pivot+1, end); }}intMainintargcChar*argv[]) {QuickSort (v1,0, V1.size ()-1); return 0;}
Data structure-sort-quick row