用C++實現七種排序演算法,可選擇排序方法,簡單易懂。
阿新 • • 發佈:2019-01-06
最近學習演算法,先從簡答的開始學起,用C++做了一個實現七種排序演算法的介面,可選擇想要用的選擇演算法,不過,由於時間倉促,沒有來得及優化和程式碼註釋,後期還會加上程式碼註釋,隨便優化一下程式碼,提高執行效率;
/************************************************************************* > File Name: sort.cpp > Author: kanty > Email: [email protected] > Created Time: Mon 24 Feb 2014 08:41:08 PM CST ************************************************************************/ #include<iostream> #define NUM 50 using namespace std; int Division(int a[],int left,int right) { int base=a[left]; while(left<right) { while(left<right && a[right]>base) --right; a[left]=a[right]; while(left<right && a[left]<base) ++left; a[right]=a[left]; } a[left]=base; return left; } void InsertSort(int a[],int m) { int i,j,t; for(i=1;i<m;i++) { t=a[i]; for(j=i-1;j>=0 && t<a[j];j--) a[j+1]=a[j]; a[j+1]=t; } } void BubbleSort(int a[],int m) { int i,j,temp; for(i=0;i<m-1;i++) { for(j=0;j<m-1-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } /* * void BubbleSort(int a[],int m) * { * int i,j,temp; * for(i=0;i<m-1;i++) * { * for(j=m-1;j>i;j--) * { * if(a[j]<a[j-1]) * { * temp=a[j]; * a[j]=a[j-1]; * a[j-1]=temp; * } * } * } * } */ void SelectSort(int a[],int m) { int i,j,k,temp; for(i=0;i<m-1;i++) { k=i; for(j=i+1;j<m;j++) if(a[k]>a[j]) k=j; temp=a[i]; a[i]=a[k]; a[k]=temp; } } void QuickSort(int a[],int left,int right) { int i,j; if(left<right) { i=Division(a,left,right); QuickSort(a,left,i-1); QuickSort(a,i+1,right); } } void ShellSort(int a[],int m) { int i,j,x,d; d=m/2; while(d>=1) { for(i=d;i<m;i++) { x=a[i]; j=i-d; while(j>=0 && a[j]>x) { a[j+d]=a[j]; j=j-d; } a[j+d]=x; } d=d/2; } } void HeapAdjust(int a[],int s,int n) { int j,t; while(2*s+1<n) { j=2*s+1; if((j+1)<n) { if(a[j]<a[j+1]) j++; } if(a[s]<a[j]) { t=a[s]; a[s]=a[j]; a[j]=t; s=j; } else break; } } void HeapSort(int a[],int m) { int t,i; int j; for(i=m/2-1;i>=0;i--) HeapAdjust(a,i,m); for(i=m-1;i>0;i--) { t=a[0]; a[0]=a[i]; a[i]=t; HeapAdjust(a,0,i); } } void MergeStep(int a[],int r[],int s,int m,int n) { int i,j,k; i=s; k=s; j=m+1; while(i<=m && j<=n) { if(a[i]<=a[j]) r[k++]=a[i++]; else r[k++]=a[j++]; } while(i<=m) r[k++]=a[i++]; while(j<=n) r[k++]=a[j++]; } void MergePass(int a[],int r[],int n,int len) { int s,e; s=0; while(s+len<n) { e=s+2*len-1; if(e>=n) e=n-1; MergeStep(a,r,s,s+len-1,e); s=e+1; } if(s<n) for(;s<n;s++) r[s]=a[s]; } void MergeSort(int a[],int m) { int *p; int len=1; int flag=0; if(!(p= new int(sizeof(int)*m))) { cout<<"failed allert!!!"<<endl; } while(len<m) { if(flag) MergePass(p,a,m,len); else MergePass(a,p,m,len); len*=2; flag=1-flag; } if(flag) for(flag=0;flag<m;flag++) a[flag]=p[flag]; delete (p); } void choose(int a[],int m) { int i; cout<<endl<<"************* sort ***************"<<endl<<endl; cout<<"******* 1---InsertSort ********"<<endl; cout<<"******* 2---BubbleSort ********"<<endl; cout<<"******* 3---SelectSort ********"<<endl; cout<<"******* 4---QuickSort ********"<<endl; cout<<"******* 5---ShellSort ********"<<endl; cout<<"******* 6---HeapSort ********"<<endl; cout<<"******* 7---MergeSort ********"<<endl<<endl; cout<<"************************************"<<endl; cout<<"please choose a sort algorithm: "; cin>>i; switch(i) { case 1 : InsertSort(a,m);break; case 2 : BubbleSort(a,m);break; case 3 : SelectSort(a,m);break; case 4 : QuickSort(a,0,m-1);break; case 5 : ShellSort(a,m);break; case 6 : HeapSort(a,m);break; case 7 : MergeSort(a,m);break; default : break; } } int main() { int a[NUM],n; cout<<"please input n :"<<endl; cin>>n; cout<<"please input "<<n<<" numbers:"<<endl; for(int i=0;i<n;i++) cin>>a[i]; cout<<"before sorted:"<<endl; for(int i=0;i<n;i++) cout<<a[i]<<" "; //cout<<"please choose a sort algorithm:"<<endl; choose(a,n); cout<<"after sorted:"<<endl; for(int i=0;i<n;i++) cout<<a[i]<<" "; return 0; }