@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@直接插入排序@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#include#include #include #include using namespace std;void Swap (int T[],int a);int main(){ int n; cin>>n; int T[n]; for(int i=0;i >T[i],i++); for(int i=1;i =0;i--) { if(T[i]>j) T[i+1]=T[i]; else break; } if(i!=n) T[i+1]=j;}@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@直接插入排序@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@二叉法插入排序@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#include #include #include using namespace std;int Find(int T[],int n){ int a=1,b=n-1; //A.B搞混; T[0]=T[n]; while(a!=b) { if(T[(a+b)/2]>=T[0]&&T[(a+b)/2-1]<=T[0]) a=b=(a+b)/2; else if(T[(a+b)/2] T[0]) b=(a+b)/2-1 ; } for(int i=n;i>a;i--) T[i]=T[i-1]; T[a]=T[0];}int main(){ int n; cin>>n; int T[n+1]; for(int i=1;i<=n;i++) cin>>T[i]; for(int i=2;i<=n;i++) { if(T[i] #include #include using namespace std;void display(int T[],int n){ for(int i=1;i<=n;i++) cout< <<" "; cout< >n; int T[n+1],increment=n; for(int i=1;i<=n;i++) cin>>T[i]; do { increment=increment/2; for(int i=increment+1;i<=n;i++) { if(T[i] 0;j-=increment) { T[j+increment]=T[j]; if(T[0]>=T[j]) break; } T[j+increment]=T[0]; } } display(T,n); } while(increment>1) ;}@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@快速排序 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#include #include #include using namespace std;int n; //写个不好 没有优化 用于display 中 说明数组大小struct So{ int size ; int *L;};void display(int T[]){ for(int i=1;i<=n;i++) cout< <<" "; cout< =T[0]&&high!=low;high--); T[j]=T[high]; j=high; for(;T[low]<=T[0]&&high!=low;low++); T[j]=T[low]; j=low; } T[low]=T[0]; display(T); return low;}void Sort(int T[],int low,int high){ if(low>=high) return ; //很重要 因为有可能 low>high ,因为如果Sort未排序 则pivotkey=high; int pivotkey; pivotkey=Partition(T,low,high); Sort(T,low,pivotkey-1); //如果pivotkey=low 则pivotkey-1 >T.size; n=T.size; T.L=new int [T.size+1]; for(int i=1;i<=T.size;i++) cin>>T.L[i]; Quick(T);}@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@堆排序 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#include #include #include #include using namespace std;int n; //写个不好 没有优化 用于display 中 说明数组大小struct So{ int size ; int *L;};void display(int T[]){ for(int i=1;i<=n;i++) cout< <<" "; cout< >T.size; n=T.size; T.L=new int [T.size+1]; for(int i=1;i<=T.size;i++) cin>>T.L[i]; for(int i=n/2;i>0;i--) heapsort(T.L,i,n); for(int i=n;i>1;i--) { swap(T.L[1],T.L[i]); heapsort(T.L,1,i-1); } display(T.L);}@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@堆排序 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#include #include #include #include using namespace std;int N;void Merge(int A[],int B[],int m,int s,int n){ int enda=s,endb=n,i=m; s++; while(m<=enda&&s<=endb) { if(B[m]<=B[s]) A[i++]=B[m++]; else A[i++]=B[s++]; } if(m<=enda) for(;i<=n;) A[i++]=B[m++]; else for(;i<=n;) A[i++]=B[s++];}void MSort(int sr[],int th1[],int m,int n){ if(m==n) th1[m]=sr[m]; else //th1 是另一个的 th2 ; { int th2[N]; int s=(m+n)/2 ; MSort(sr,th2,m,s); MSort(sr,th2,s+1,n); Merge(th1,th2,m,s,n); }}int main(){ int n; cin>>n; N=n+1; int T[n+1],S[n+1]; for(int i=1;i<=n;i++) cin>>T[i]; MSort(T,S,1,n); for(int i=1;i<=n;i++) cout< <<" "; cout<#include #include #include using namespace std;int N;void Merge(int A[],int B[],int m,int s,int n){ int enda=s,endb=n,i=m; s++; while(m<=enda&&s<=endb) { if(B[m]<=B[s]) A[i++]=B[m++]; else A[i++]=B[s++]; } if(m<=enda) for(;i<=n;) A[i++]=B[m++]; else for(;i<=n;) A[i++]=B[s++];}void MSort(int sr[],int th1[],int m,int n){ if(m==n) th1[m]=sr[m]; else //th1 是另一个的 th2 ; { int th2[N]; int s=(m+n)/2 ; MSort(sr,th2,m,s); MSort(sr,th2,s+1,n); Merge(th1,th2,m,s,n); }}int main(){ int n; cin>>n; N=n+1; int T[n+1],S[n+1]; for(int i=1;i<=n;i++) cin>>T[i]; MSort(T,S,1,n); for(int i=1;i<=n;i++) cout< <<" "; cout<#include #include #include using namespace std;int N;void display(int T[]){ for(int i=1;i<=N;i++) cout< <<" "; cout< =length) break; Mergepass(th,T,k,length); k*=2; }}int main(){ int n; cin>>n; N=n; int T[n+1]; for(int i=1;i<=n;i++) cin>>T[i] ; MSort(T,n) ;}