主题:[求助]为什么不能排序????????
昨天写了个递归的归并排序算法,可执行后输出的却和原来的一样,根本就没有进行排序,不知是哪错了?请各位指点.
下面是我的程序:
void myMerge(Datatype olddata[],Datatype newdata[],int p,int m,int n)
{
int i,j,k,a;
i=p; j=m+1; k=p;
while ((i<=m)&&(j<=n))
if (olddata[i]<=olddata[j])
newdata[k++]=olddata[i++];
else newdata[k++]=olddata[j++];
if (i<m+1) for (a=i;a<=m;a++) newdata[k++]=olddata[a];
if (j<n+1) for (a=j;a<=n;a++) newdata[k++]=olddata[a];
}
void myMergesort(Datatype olddata[],Datatype newdata[],int s,int t)
{
int mid=(s+t)/2;
if (s<=t)
if (s==t) newdata[s]=olddata[s];
else
{
myMergesort(olddata,newdata,s,mid);
myMergesort(olddata,newdata,mid+1,t);
myMerge(olddata,newdata,s,mid,t);
}
else return;
}
void myMergeSortALL(Datatype a[],int n)
{
Datatype *swap=new Datatype[n];
myMergesort(a,swap,0,n-1);
delete []swap;
cout<<endl<<"完成了递归的归并排序……"<<endl;
}
下面是我的程序:
void myMerge(Datatype olddata[],Datatype newdata[],int p,int m,int n)
{
int i,j,k,a;
i=p; j=m+1; k=p;
while ((i<=m)&&(j<=n))
if (olddata[i]<=olddata[j])
newdata[k++]=olddata[i++];
else newdata[k++]=olddata[j++];
if (i<m+1) for (a=i;a<=m;a++) newdata[k++]=olddata[a];
if (j<n+1) for (a=j;a<=n;a++) newdata[k++]=olddata[a];
}
void myMergesort(Datatype olddata[],Datatype newdata[],int s,int t)
{
int mid=(s+t)/2;
if (s<=t)
if (s==t) newdata[s]=olddata[s];
else
{
myMergesort(olddata,newdata,s,mid);
myMergesort(olddata,newdata,mid+1,t);
myMerge(olddata,newdata,s,mid,t);
}
else return;
}
void myMergeSortALL(Datatype a[],int n)
{
Datatype *swap=new Datatype[n];
myMergesort(a,swap,0,n-1);
delete []swap;
cout<<endl<<"完成了递归的归并排序……"<<endl;
}