1#include <iostream>2#include <cstdlib>3#include <ctime>4 using namespacestd;5 Const intLen = -;6 //1. Bubble sort7 voidBubble_sort (intS[],intLen) {8 BOOLFlag;9 for(intI=1; i<len-1;++i) {Tenflag=false; One for(intj=1; j<=len-i;++j) A if(s[j]>s[j+1]) {flag=true; Swap (s[j],s[j+1]);} - if(!flag) Break; - } the } - //2. Select Sort - voidSelect_sort (intS[],intLen) { - intK; + for(intI=1; i<len;++i) { -k=i; + for(intj=i+1; j<=len;++j) A if(S[k]>s[j]) k=J; at if(k!=i) swap (s[i],s[k]); - } - } - //3. Insert Sort - voidInsert_sort (intS[],intLen) { - for(intI=2; i<=len;++i) { in inttmp=s[i],j; - for(j=i-1;j>0&&tmp<s[j];--j); to for(intk=i;k>j+1;--k) s[k]=s[k-1]; +s[j+1]=tmp; - } the } * //4. Quick Sort $ intQuick_sort (intS[],intLowintHigh ) {Panax Notoginseng intTmp=s[low];//the first element is the pivot - while(Low//The sequence to be sorted is longer than 1 the while(Low//elements that are larger than the pivot are still on the right +S[low]=s[high];//Leave elements smaller than the pivot to the left A while(Low//elements that are smaller than the pivot are still on the left theS[high]=s[low];//put elements that are larger than the pivot to the right + } -s[low]=tmp;//pivot record in place $ returnLow//returns the position of the pivot $ } - voidQsort (intS[],intLowintHigh ) { - if(low<High ) { the intkey=Quick_sort (s,low,high); -Qsort (s,low,key-1);WuyiQsort (s,key+1, high); the } - } Wu //5. Hill sort (with direct insertion) - voidShell_sort (intS[],intLen) { About for(intstep=len/2;step>0; step/=2){//Set Step size $ for(inti=step;i<=len;++i) { - inttmp=s[i],j; - for(j=i-step;j>0&&tmp<s[j];j-=step); - for(intK=i;k>j+step;k-=step) s[k]=s[k-step]; As[j+step]=tmp; + } the } - } $ //6. Merge sort the voidMerge (intS[],intT[],intLowintMidintHigh ) { the intI=low,j=mid+1, k=Low ; the while(i<=mid&&j<=High ) { the if(S[i]<=s[j]) t[k++]=s[i++]; - Elset[k++]=s[j++]; in } the while(I<=mid) t[k++]=s[i++]; the while(J<=high) t[k++]=s[j++]; About for(intI=low;i<=high;++i) S[i]=t[i];//copies the interval [Low,high] to the position of the original array A, indicating that the interval element is already sequenced . the } the voidMerge_sort (intS[],intT[],intLowintHigh ) { the if(low<High ) { + intMid= (Low+high)/2; -Merge_sort (S,t,low,mid);//recursive into the left part theMerge_sort (s,t,mid+1, high);//recursive into the right partBayiMerge (S,t,low,mid,high);//merge the two parts the } the } - //Print Array Values - voidPrintintS[],intLen) { the for(intI=1; i<=len;++i) thecout<<s[i]<< (I==len?"\ n":" "); the } the - intMain () { the int*s=New int[len+1]; the int*t=New int[len+1];//T is an auxiliary array the Srand ((unsigned) time (NULL));94 for(intI=1; i<=len;++i) s[i]=rand (); thePrint (S,len);//print the original array the /*1. Bubble sort the Bubble_sort (S,len);*/98 /*2. Select Sort About Select_sort (S,len);*/ - /*3. Insert Sort101 Insert_sort (S,len);*/102 /*4. Quick Sort103 Qsort (S,1,len);*/104 /*5. Hill Sort the Shell_sort (S,len);*/106 /*6. Merge sort107 Merge_sort (S,t,1,len);*/108Print (S,len);//the sorted array109 the Delete[]s;//Freeing Memory111 Delete[]t; the return 0;113}
Seven sorting algorithms (update ... )