-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmerge_sort.c
127 lines (112 loc) · 2.74 KB
/
merge_sort.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Copyright © https://github.com/jarry All rights reserved.
* @author: [email protected]
* @version: 1.0
*/
#include <stdio.h>
#include <math.h>
#include <time.h>
void merge(int arr[], int left, int mid, int right);
void merge_sort(int arr[], int left, int right);
// 合并两个子数组,根据中间值分为2个数组
void merge(int arr[], int left, int mid, int right)
{
// 初始化左右子数组长度
int left_len = mid - left + 1;
int right_len = right - mid;
// 初始化两个子数组
int left_arr[left_len], right_arr[right_len];
/* 复制数据到子数组,根据中间数来复制 */
for (int i = 0; i < left_len; i++)
{
left_arr[i] = arr[left + i];
}
for (int j = 0; j < right_len; j++)
{
right_arr[j] = arr[mid + 1 + j];
}
int i = 0;
int j = 0;
// 合并到数组,从左侧位置开始合并
int idx = left;
while (i < left_len && j < right_len)
{
if (left_arr[i] <= right_arr[j])
{
// 左侧小于右侧,则复制左侧成员
arr[idx] = left_arr[i];
i++;
}
else
{
// 否则复制右侧成员到原数组
arr[idx] = right_arr[j];
j++;
}
idx++;
}
/* 复制左侧剩余成员 */
while (i < left_len)
{
arr[idx] = left_arr[i];
i++;
idx++;
}
/* 复制右侧剩余成员 */
while (j < right_len)
{
arr[idx] = right_arr[j];
j++;
idx++;
}
}
// 递归调用左右侧进行数组合并
void merge_sort(int arr[], int left, int right)
{
if (left < right)
{
// 中间值为左+右/2取整
int mid = left + (right - left) / 2;
// 递归合并左侧
merge_sort(arr, left, mid);
// 递归合并右侧
merge_sort(arr, mid + 1, right);
// 合并排序
merge(arr, left, mid, right);
}
}
void print_array(int *arr, int len)
{
printf("{ ");
for (int i = 0; i < len; i++)
{
printf("%d", arr[i]);
if (i < len - 1)
{
printf(", ");
}
}
printf(" }");
}
int main()
{
int arr[7] = {7, 11, 9, 10, 12, 13, 8};
int len = sizeof(arr) / sizeof(arr[0]);
printf("Origin array: \n");
print_array(arr, len);
float start_time = clock();
merge_sort(arr, 0, len - 1);
printf("\nSorted array: \n");
print_array(arr, len);
printf("\ntime: %f ms.", ((clock() - start_time) / CLOCKS_PER_SEC * 1000));
return 0;
}
/**
jarry@jarrys-mbp mergesort % gcc merge_sort.c
jarry@jarrys-mbp mergesort % ./a.out
Origin array:
{ 7, 11, 9, 10, 12, 13, 8 }
Sorted array:
{ 7, 8, 9, 10, 11, 12, 13 }
time: 0.016000 ms.%
*/