c语言实现二分归并排序
时间: 2023-11-05 11:59:03 浏览: 121
c语言实现二分归并排序的代码如下所示:
```c
#include<stdio.h>
void merge(int arr[], int start, int mid, int end){
int temp[10];
int i = start;
int j = mid + 1;
int k = 0;
while(i <= mid && j <= end){
if(arr[i] <= arr[j]){
temp[k++] = arr[i++];
}
else{
temp[k++] = arr[j++];
}
}
while(i <= mid){
temp[k++] = arr[i++];
}
while(j <= end){
temp[k++] = arr[j++];
}
for(int m = 0; m < k; m++){
arr[start + m] = temp[m];
}
}
void mergeSort(int arr[], int start, int end){
if(start >= end){
return;
}
int mid = (start + end) / 2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, mid, end);
}
int main(){
int arr[] = {5, 2, 8, 3, 1, 9, 4, 7, 6};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
printf("Sorted array: \n");
for(int i = 0; i < n; i++){
printf("%d ", arr[i]);
}
return 0;
}
```
阅读全文