-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.cpp
67 lines (58 loc) · 1.08 KB
/
merge_sort.cpp
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
#include <cstdio>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <list>
#include <set>
#include <map>
using namespace std;
// merge sort very useful also can add comparator.
const int N = 10010;
int a[N], aa[N];
inline bool cmp(int a, int b) {
return a > b;
}
inline void mergeIt(int l, int mid, int r) {
int i = l, j = mid + 1, k = l;
while (i <= mid && j <= r) {
if (cmp(a[i], a[j])) {
aa[k++] = a[i++];
} else {
aa[k++] = a[j++];
}
}
while (i <= mid) {
aa[k++] = a[i++];
}
while (j <= r) {
aa[k++] = a[j++];
}
for (int ii = l; ii <= r; ii++) {
a[ii] = aa[ii];
}
}
inline void mergeSort(int l, int r) {
if (l < r) {
int mid = (l + r) >> 1;
mergeSort(l, mid);
mergeSort(mid + 1, r);
mergeIt(l, mid, r);
}
}
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", a + i);
}
mergeSort(0, n - 1);
for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
}