附录: 归并排序的C++语言描述 #includevoid Merge Sort(T a[l, int left, int right); templatevoid Merge(tcl, Td[, int l, int m, int r); templatevoid Copy (tal,T bl, int l, int r) void maino int const n(5) int an]; cout void Merge Sort(T a[l, int left, int right)// if(left<right) int i=(left+right) /2 T*b=new TI Merge Sort(a, left, i); Merge Sort(a, i+l, right); Merge(a, b, left, i, right) Copy(a, b, left, right)
附录: 归并排序的 C++语言描述 #include templatevoid MergeSort(T a[],int left,int right); templatevoid Merge(T c[],T d[], int l,int m,int r); templatevoid Copy(T a[],T b[],int l,int r); void main() { int const n(5); int a[n]; cout>a[i]; //for(int j=0;j void MergeSort(T a[],int left,int right) // { if(left<right) { int i=(left+right)/2; T *b=new T[]; MergeSort(a,left,i); MergeSort(a,i+1,right); Merge(a,b,left,i,right); Copy(a,b,left,right); }
template void Merge(tcl,t dI, int I, int m, int r) nt int j=m+I int kl while((im) for(int q=j; q void Copy(tal,t b[, int I, int r) for(int i-l; Kvoid Quick Sort(T a[l, int p, int r) templateint Partition(T[, int p, int r)
} template void Merge(T c[],T d[],int l,int m,int r) { int i=l; int j=m+1; int k=l; while((im) { for(int q=j;q void Copy(T a[],T b[], int l,int r) { for(int i=l;i templatevoid QuickSort(T a[],int p,int r); templateint Partition(T a[],int p,int r);
void maino int const n(5) int a[n] cout>all Quick Sort(a,0n-1) cout void QuickSort(T[l, int p, int r) if(p int Partition(T a[l, int p, int r) int i= j=r+I while(true) while(a[++i]x);
void main() { int const n(5); int a[n]; cout>a[i]; QuickSort(a,0,n-1); cout void QuickSort(T a[],int p,int r) { if(p int Partition(T a[],int p,int r) { int i=p,j=r+1; T x=a[p]; while(true) { while(a[++i]x); if(i>=j)break;
Swap(aj], aD) ap=all return template inline void Swap(T &s,T&t)
Swap(a[i],a[j]); } a[p]=a[j]; a[j]=x; return j; } template inline void Swap(T &s,T &t) { T temp=s; s=t; t=temp; }