-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeSort.c
45 lines (42 loc) · 1.1 KB
/
mergeSort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
/* 归并排序 */
/* 合并两个有序段 两个有序段在同一个数组中的不同位置 */
void merge(int* a, int left, int mid, int right, int *c){
int i = left, j = mid+1;
int m = mid, n = right;
int k=0;
while(i<=m && j<=n){
if(a[i] <= a[j])
c[k++] = a[i++];
else
c[k++] = a[j++];
}
while(i<=m)
c[k++] = a[i++];
while(j<=n)
c[k++] = a[j++];
for(i=0; i<k; i++)
a[left+i] = c[i];
} //临时数组
void mergeSort0(int* arr, int left, int right, int* tmp){
if(left < right){//最少要剩下两个元素
int mid = (left + right) / 2;
mergeSort0(arr, left, mid, tmp);
mergeSort0(arr, mid+1, right, tmp);
merge(arr, left, mid, right, tmp);
}
}
void mergeSort(int* arr, int len){
int* tmp = (int*)malloc(sizeof(int)*len);
mergeSort0(arr, 0, len-1, tmp);
free(tmp);
}
int main(){
int arr[] = {5,3,9,8,2,7,6,1,4,10};
mergeSort(arr, 10);
int i;
for(i=0; i<10; i++)
printf("%d ", arr[i]);
return 0;
}