偶写了个归并排序:
#include <stdio.h>#define LEN 5int a[LEN] = { 10, 5, 2, 4, 7 };int t[LEN];
void merge_sort(int start, int end){ if (start < end) { int mid = (start + end) / 2; int i = start, j = mid, k = start; merge_sort(start, mid); merge_sort(mid+1, end); while (i < mid || j <= end) if (j == end + 1 || (i < mid && a[i] < a[j])) t[k++] = a[i++]; else t[k++] = a[j++]; for (i = 0; i <= end; i++) a[i] = t[i]; }}int main(void){ int i; merge_sort(0, LEN-1); for (i = 0; i < LEN; i++) printf("%d ", a[i]); printf("\n"); return 0;}但是运行结果不对- -!找了N久问题也没找出,好伤心啊- -!请各位帮助,谢谢。
#include <stdio.h>#define LEN 5int a[LEN] = { 10, 5, 2, 4, 7 };int t[LEN];
void merge_sort(int start, int end){ if (start < end) { int mid = (start + end) / 2; int i = start, j = mid, k = start; merge_sort(start, mid); merge_sort(mid+1, end); while (i < mid || j <= end) if (j == end + 1 || (i < mid && a[i] < a[j])) t[k++] = a[i++]; else t[k++] = a[j++]; for (i = 0; i <= end; i++) a[i] = t[i]; }}int main(void){ int i; merge_sort(0, LEN-1); for (i = 0; i < LEN; i++) printf("%d ", a[i]); printf("\n"); return 0;}但是运行结果不对- -!找了N久问题也没找出,好伤心啊- -!请各位帮助,谢谢。