//MergeSort.cpp
#include
#include
#define MAXSIZE 20
#define LENGTH 7
typedef int RedType;
typedef struct //SqList structure
{ RedType r[MAXSIZE+1]; //Records Type
int length;
}SqList;
typedef SqList RcdType;
void Merge(RcdType SR,RcdType &TR,int i,int m,int n) //Merge() function
{ int j,k;
for(j=m+1,k=i;i<=m&&j<=n;++k)
{ if(SR.r[i]<=SR.r[j])
TR.r[k]=SR.r[i++];
else
TR.r[k]=SR.r[j++];
}
while(i<=m)
TR.r[k++]=SR.r[i++];
while(j<=n)
TR.r[k++]=SR.r[j++];
}//end of Merge() function
void MSort(RcdType SR,RcdType &TR1,int s, int t) //MSort() function
{ int m;
RcdType TR2;//[LENGTH];
if(s==t)
TR1.r[s]=SR.r[t];
else
{ m=(s+t)/2;
MSort(SR,TR2,s,m);
MSort(SR,TR2,m+1,t);
Merge(TR2,TR1,s,m,t);
}//end of else
}//end of MSort() function
void MergeSort(SqList &L) //MergeSort() function
{
MSort(L,L,1,L.length);
}//end of MergeSort() function
void main() //main function
{ int i;
SqList L;//={{0,49,38,65,97,76,13,27,},LENGTH};
cout<<"MergeSort.cpp"<
cin>>L.length;
cout<<"Please input the disordered array L.r:
cin>>L.r[i];
MergeSort(L);
cout<
cout<
}//end of main() function我以前的,可以调试的
应该符合你要求,只是很少部分你自己改一下,比如数的个数
输入改为 rand()随即输入,刚才粘贴错了
归并排序:需要两个有序链表,生成一个有序的包含两个原链表节点的排序
楼主一个链表,想怎么进行归并排序呢