算法1:
void Merge(int numa[],int numb[],int tmp[],int l,int r,int leftend,int rightend,int max)
{
//l the beginning of the first array
//r the beginning of the second array
//max the size of the temporary array
int count=0;
while (l!=leftend&&r!=rightend)// both of the array have not come to the end
{
if(numa[l]<numb[r])
{
tmp[count++] = numa[l++];
}
else
{
tmp[count++] = numb[r++];
}
}
while(l<leftend)
{
tmp[count++] = numa[l++];
}
while(r<rightend)
{
tmp[count++] = numb[r++];
}
}
算法2 递归
void MSort(ElementType A[],ElementType TmpA[],int L,int RightEnd)
{
int center;
if(L<RightEnd)
{
center = (L + RightEnd) / 2;
MSort(A, TmpA, L, center);
MSort(A, TmpA, center + 1, RightEnd);
Merge(A, TmpA, L, center + 1, RightEnd);
}
}