分治法 合并排序算法 但是为什么输出不对呀
#include<stdio.h>#define M 100
int merge(int a[], int start, int mid, int end){
int b[M];
int i, j, k = 0;
i = start;
j = mid + 1;
while(i<=mid && j<=mid){
if(a[i] <= a[j])
b[k++] = a[i++];
else
b[k++] = a[j++];
}
while(i <= mid)
b[k++] = a[i++];
while(j <= end)
b[k++] = a[j++];
for(i=0; i<k; i++)
a[i] = b[i];
return 1;
}
int mergeSort(int a[], int start, int end){
int mid;
if(start < end){
mid = (start + end) / 2;
mergeSort(a, start, mid);
mergeSort(a, mid+1, end);
merge(a, start, mid, end);
}
return 1;
}
int main(){
int a[M];
int i;
int n;
scanf("%d", &n);
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
mergeSort(a, 0, n);
for(i=0; i<n; i++){
printf("%d ", a[i]);
}
return 0;
}
/*
感觉写的没毛病 我想问题可能出在merge函数的最后面那个循环,可我不知道怎么改....有大佬知道我问题出在哪里吗?
*/