自己按照书上算法编的,可以运行,不过结果好象不对,请大家帮忙看下问题出在哪里
/* 二路归并排序*/
/*最后修改2007-1-1*/
#include<stdio.h>
#define MAXNUM 20
#define TRUE 1
#define FALSE 0
typedef int KeyType;
typedef struct
{
KeyType key;
}RedType;
typedef struct
{
int n;
RedType record[MAXNUM];
} SqList;
void Merge(RedType SR[], RedType TR[], int i, int m, int n)
{
int j,k;
for ( j = m + 1,k=i;i <= m && j <=n;++k )/*将SR中记录由小到大地并入TR*/
{
if (SR[i].key <= SR[j].key)
TR[k] = SR[i++];
else TR[k] = SR[j++];
}
if(i <= m) TR[k++] = SR[i++]; /*将剩余的SR[i..m]复制到TR*/
if(j <= n) TR[k++] = SR[j++]; /*将剩余的SR[j..n]复制到TR*/
}/*将有序的SR[i..m]和SR[m+1..n]归并为有序的TR[i..n]*/
void MSort(RedType SR[], RedType TR1[], int s, int t)
{
int m;
RedType TR2[MAXNUM];
if(s==t) TR1[s]=SR[s];
else
{
m=(s+t)/2; /*将SR[s..t]平分为SR[s..m]和SR[m+1..t]*/
MSort(SR,TR2,s,m); /*递归地将SR[s..m]归并为有序的TR2[s..m]*/
MSort(SR,TR2,m+1,t); /*递归地将SR[m+1..t]归并为有序的TR2[m+1..t]*/
Merge(TR2,TR1,s,m,t);/*将TR2[s..m]和TR2[m+1..t]归并到TR1[s..t]*/
}
}/*将SR[s..t]归并排序为TR1[s..t]*/
void MergeSort(SqList L)
{
MSort(L.record,L.record,1,L.n);
}/*对顺序表L作归并排序*/
int main(){
int i;
SqList List = {7,5,3,2,6,8,9,4,1};
MergeSort(List);
for(i = 0; i < 8; i++)
printf("%d ", List.record[i]);
getchar();
return 0;
}