1.顺序很重要:
合并排序递归算法具体步骤:
#ifndef MERGESORT_H_INCLUDED
#define MERGESORT_H_INCLUDED
#includeusing namespace std;
templatevoid Copy(T *a,T *b,int l,int r){
for(int i=l;i<=r;i++){
a[i]=b[i];
}
}
templatevoid Merge(T *c,T *d,int l,int m,int r){
int i=l, j=m+1,k=l;
while((i<=m)&&(j<=r)){
if(c[i]<=c[j])
d[k++] =c[i++];
else
d[k++] =c[j++];
}
if(i>m){
for(int q=j;q<=r;q++){
d[k++] =c[q];
}
} else{
for(int q=i;q<=m;q++){
d[k++] =c[q];
}
}
template
void MergeSort(T *a,T *b,int left,int right){
if(left < right) {
int i = (left+right)/2;
MergeSort(a,b,left,i);
MergeSort(a,b,i+1,right);
Merge(a,b,left,i,right);
Copy(a,b,left,right);
}
}
#endif // MERGESORT_H_INCLUDED
以上程序是正确的,但是在编程的时候,我不小心把Copy函数放在了MergeSort函数的后面导致程序中一直出现’Copy’ was was not declared in this scope ,and no declarations…