归并排序算法实现如下
void merge(int *a, int *b, int i, int m, int n) {
int t, k, j;
for(j=m+1, k=i; i<=m && j<=n; k++) {
if(a[i] < a[j]) b[k] = a[i++];
else b[k] = a[j++];
}
if(i <= m) for(t=i; t<=m; t++)b[k++] = a[t];
if(j <= n) for(t=j; t<=n; t++) b[k++] = a[t];
}
void mSort(int *a, int *b, int s, int t) {
int m; static int b1[N]; //只初始化一次
if(s == t) b[s] = a[s];
else {
m = (s + t) / 2; //将a[s..t]平分为a[s..m]和a[m+1..t]
mSort(a, b1, s, m); //递归地将a[s..m]归并为有序的b1[s..m]
mSort(a, b1, m+1, t); //递归地将a[m+1..t]归并为有序的b1[m+1..t]
merge(b1, b, s, m, t); //将b1[s..m]和b1[m+1..t]归并到b[s..m]
}
}
void mergeSort(int *a) {
mSort(a, a, 0, N-1);
}