-
Notifications
You must be signed in to change notification settings - Fork 1
/
msort.c
46 lines (41 loc) · 862 Bytes
/
msort.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
int buf[2][100];
// sort [l, r)
void merge_sort(int low, int high)
{
if (low + 1 >= high) { return; }
int mid = (low + high) / 2;
merge_sort(low, mid);
merge_sort(mid, high);
int i = low, j = mid, k = low;
while (i < mid && j < high) {
if (buf[0][i] < buf[0][j]) {
buf[1][k] = buf[0][i];
i = i + 1;
} else {
buf[1][k] = buf[0][j];
j = j + 1;
}
k = k + 1;
}
while (i < mid) {
buf[1][k] = buf[0][i];
i = i + 1;
k = k + 1;
}
while (j < high) {
buf[1][k] = buf[0][j];
j = j + 1;
k = k + 1;
}
while (low < high) {
buf[0][low] = buf[1][low];
low = low + 1;
}
}
int main()
{
int n = getarray(buf[0]);
merge_sort(0, n);
putarray(n, buf[0]);
return 0;
}