The basic idea of selecting a sort is: one of the most value lookups, and then sort
//The simple InsertsortwayvoidSelectsort (int*a) { intn =strlen (a); for(intK k<n; k++){ intL =K; for(intJ j<k; J + +){ if(A[j] >A[l]) {L=J; } } intTMP =A[k]; A[K]=A[l]; A[L]=tmp; } }//The nice insertsortwayvoidSelectsort (intR[],intN) {intI, J, Min, Max, TMP; for(i=1; I <= n/2; i++) {min= i; Max =i; for(j= i+1; J<= n-i; J + +) { if(R[j] >R[max]) {Max= J;Continue ; } if(r[j]<R[min]) {min=J; }} tmp= r[i-1]; r[i-1] = R[min]; R[min] =tmp; TMP= R[n-i]; R[n-i] = R[max]; R[max] =tmp; } }
Quick Sort
voidPrintintA[],intN) { for(intj=0; j<n; J + +) {cout<<A[J] <<" "; } cout<<Endl; } voidSwapint*a,int*b) {intTMP = *A; *a = *b; *b =tmp; } intPartitionintA[],intLowintHigh ) { intPrivotkey = A[low];//Datum elements while(Low < High) {//alternately scan from both ends of the table to the middle while(Low < High && A[high] >= privotkey)--high;//search forward from the point of high, up to the low+1 position. Swap smaller than the base element to the low endSwap (&a[low), &A[high]); while(Low < High && A[low] <= privotkey) + +Low ; Swap (&a[low], &A[high]); } print (A,Ten); returnLow ; } voidQsort_improve (intR[],intLowintHighintk) { if(High-Low > K) {//recursion at length greater than K, K for specified number intPivot = partition (R, Low, high);//the partition algorithm that is called remains the sameQsort_improve (R, Low, pivot-1, K); Qsort_improve (R, Pivot+1, high,k); } } voidQuickSort (intR[],intNintk) {Qsort_improve (R,0, n,k);//first call the improved algorithm qsort make it basically orderly//sort the basic ordered sequence with insert sort for(intI=1; I<=n;i + +){ intTMP =R[i]; intj=i-1; while(TMP <R[j]) {R[j+1]=R[J]; j=j-1; } r[j+1] =tmp; } } intMain () {inta[Ten] = {3,1,5,7,2,4,9,6,Ten,8}; cout<<"Initial value:"; Print (A,Ten); QuickSort (A,9,4); cout<<"Results:"; Print (A,Ten); }
Classic Sorting algorithm review: Select sort, Quick sort