文档介绍:什么是合并排序 合并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(DivideandConquer)的一个非常典型的应用。合并排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为2-路归并。合并排序也叫归并排序递归合并排序 #include<> template<classT>voidMergeSort(Ta[],intleft,intright); template<classT>voidMerge(Tc[],Td[],intl,intm,intr);template<classT>voidCopy(Ta[],Tb[],intl,intr); voidmain() { intconstn(5); inta[n]; cout<<"Input"<<n<<"numbersplease:"; for(inti=0;i<n;i++) cin>>a[i]; MergeSort(a,0,n-1); cout<<"Thesortedarrayis"<<endl; for(i=0;i<n;i++) cout<<a[i]; cout<<endl; } template<classT> voidMergeSort(Ta[],intleft,intright) { if(left<right){ inti=(right+left)/2; T*b=newT[]; MergeSort(a,left,i); MergeSort(a,i+1,right); Merge(a,b,left,i,right); Copy(a,b,left,right); } } template<classT> voidMerge(Tc[],Td[],intl,intm,intr) { inti=l,j=m+1,k=l; while(i<=m&&j<=r) { if(c[i]<=c[j])d[k++]=c[i++]; elsed[k++]=c[j++]; } if(i>m) { for(intq=j;q<=r;q++) d[k++]=c[q]; } else for(intq=i;q<=m;q++) d[k++]=c[q]; } template<classT> voidCopy(Ta[],Tb[],intl,intr) { for(inti=l;i<=r;i++) a[i]=b[i];}迭代合并排序 #include<> voidprintArray(int*head,intlen){ printf("\ndatas:"); inti=1; for(i=1;i<=len-1;i++){ printf("%d",head[i]); } printf("\n"); } voidcopyArray(int*head,int*tmp,intlen){ inti=0; for(i=0;i<=len;i++){ head[i]=tmp[i]; } } /* * * */ voidMergeTwo(int*head,int*tmp,intl,intm,