分治法 合并排序法
#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=start; i<end; 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函数后面的循环有问题,可我不知道怎么改 有大佬看看 是哪里出了问题吗
*/