比较了weiss的代码和严蔚敏的代码,觉得二者不分伯仲呐……不过weiss的代码更容易以C实现…… #include <stdio.h> #include <stdlib.h> #define MAXLEN 100 #define LQ(a,b) ((a)<=(b)) typedef int ElemType; void Merge(ElemType A[],ElemType Temp[],int left,int right,int rend){ int lend=right-1; //左半的截止点 int tempos=left; int i; int elemnum=rend-left+1; //元素总个数 while(left<=lend&&right<=rend){ if(LQ(A[left],A[right])) Temp[tempos++]=A[left++]; else Temp[tempos++]=A[right++]; }//while while(left<=lend) Temp[tempos++]=A[left++]; while(right<=rend) Temp[tempos++]=A[right++]; for(i=0;i<elemnum;i++,rend--) A[rend]=Temp[rend]; }//Merge void Msort(ElemType A[],ElemType Temp[],int left,int right){ int center; if(left<right){ center=(left+right)/2; Msort(A,Temp,left,center); Msort(A,Temp,center+1,right); Merge(A,Temp,left,center+1,right); }//if } void Mergesort(ElemType A[],int count){ ElemType *Temp; Temp=malloc((count+1)*sizeof(ElemType)); //MARK:malloc还可以这样使用,好神奇 if(Temp){ Msort(A,Temp,1,count); //0号单元不使用 free(Temp); }//if else printf("Fatal error!"); } void main(){ ElemType A[MAXLEN+1]; int count=0; //数据个数 int i; while(scanf("%d",&A[++count])); //输入数字,并以任意字符结束输入 count--; Mergesort(A,count); for(i=1;i<=count;i++) printf("%d ",A[i]); }//main