Initial state: 6 1 2 7 9 3 4 5 10 8
Process diagram for sorting
First Sentinel J started out. Because the number of datums set here is the leftmost number, it is important to have Sentinel J go first ( think about why ):: The last and the base number must be less than the number of the base number,
#include <cstdio>#include<algorithm>using namespacestd;ints[ -],n;voidQuick_sort (intLeftintright) {//the interval sort of left--right if(Left>=right)return; intTm=s[left];//Base Number inti=left,j=Right ; while(i!=j) { while(s[j]>=tm&&i<j) j--;//look to the left from the right . while(s[i]<=tm&&i<j) i++;//look from left to right if(i<j) Swap (S[i],s[j]); } swap (s[left],s[i]); Quick_sort (Left,i-1); Quick_sort (i+1, right);}intMain () {//freopen ("data.in", "R", stdin);scanf"%d",&N); for(intI=1; i<=n;i++) scanf ("%d", s+i); Quick_sort (1, n);//for (int i=1;i<=n;i++) printf ("%d", s[i]);//printf ("\ n");}
Sit on the toilet. Algorithm: Quick Sort