初学归并排序,但输出无结果...
程序代码:
#include <stdio.h> #include <malloc.h> void Merge(int c[], int d[], int l, int m, int r) { int i = 1, j = m + 1, k = l, q; while((i <= m) && (j <= r)) if(c[i] <= c[j]) d[k++] = c[i++]; else d[k++] = c[j++]; if(i > m) for(q = j; q <= r; q++) d[k++] = c[q]; else for(q = i; q <= m; q++) d[k++] = c[q]; } void Mergepass(int x[], int y[], int s, int n) { int i = 0, j; while(i <= n - 2 * s) { Merge(x, y, i, i + s - 1, i + 2 * s - 1); i = i + 2 * s; } if(i + s < n) Merge(x, y, i, i + s - 1, n - 1) ; else for(j = i; j <= n - 1; j++) y[j] = x[j]; } void MergeSort(int a[], int n) { int *temp=(int*)malloc(20), s = 1; while(s < n) { Mergepass(a, temp, s, n); s += s; Mergepass(temp, a, s, n); s += s; } } int main() { int a[] = {3, 5, 3, 6, 4, 7, 5, 7, 4}, i; MergeSort(a, 9); for(i = 0; i < 9; i++) printf("%d ", a[i]); printf("\n"); return 0; } 哪位大侠帮忙看看...