本文共 788 字,大约阅读时间需要 2 分钟。
#include#define LEN 9int a[LEN] = {43,5,2,4,7,1,3,2,6};void merge(int start,int mid,int end){ int n1 = mid - start + 1; int n2 = end - mid; int left[n1],right[n2]; int i,j,k; for(i = 0; i < n1; i++) left[i] = a[start + i]; for(j = 0; j < n2; j++) right[j] = a[mid + j + 1]; i = j = 0; k = start; while(i < n1 && j < n2) if(left[i] < right[j]) a[k++] = left[i++]; else a[k++] = right[j++]; while(i < n1) a[k++] = left[i++]; while(j < n2) a[k++] = right[j++];}void sort(int start,int end){ int mid; if(start < end) { mid = (start + end) / 2; sort(start,mid); sort(mid + 1,end); merge(start,mid,end); }}int main(){ int i; for(i = 0; i < LEN; i++) printf("%d ",a[i]); printf("\n"); sort(0,LEN - 1); for(i = 0; i < LEN; i++) printf("%d ",a[i]); printf("\n"); return 0;}
转载地址:http://cxuhj.baihongyu.com/