/*老钱著作:*/ #include "stdio.h" int a[]={4,76,2,34,5,9,8,0,34,11,45,67,99,45,67,4}; void merge(int a[],int low,int mid,int high) { int h,i,j,k,/*b[high]*/ b[16]; h=low; i=low; j=mid+1; /*当两个集合都没有取尽时*/ while ( (h<=mid) && (j<=high) ) { if (a[h]<=a[j]) { b[i]=a[h]; h=h+1; } else { b[i]=a[j]; j=j+1; } i=i+1; } /*处理剩余的元素*/ if (h>mid) { for (k=j;k<=high;k++) { b[i]=a[k]; i=i+1; } } else { for (k=h;k<=mid;k++) { b[i]=a[k]; i=i+1; } } /*将已归并的集合复制到a*/ for(k=low;k<=high;k++) { a[k]=b[k]; } } void mergesort(int a[],int low,int high) { int mid; if (low<high) { mid=(low+high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,mid,high); } } int main(int argc,char *argv[]) { mergesort(a,0,15); for (int i=0;i<16;i++) { printf(" %d ",a[i]); } printf("/n"); return 0; }