0%

Merge Sort

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
#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

void ms(int l, int r, vector<int> &vec, vector<int> &temp) {
if (l >= r) return;
int mid = l + (r - l) / 2;
ms(l, mid, vec, temp);
ms(mid + 1, r, vec, temp);
int cnt = 0;

for (int i = l, j = mid + 1; i <= mid || j <= r;) {
if (i > mid) {
temp[cnt++] = vec[j++];
} else if (j > r) {
temp[cnt++] = vec[i++];
} else if (vec[i] <= vec[j]) {
temp[cnt++] = vec[i++];
} else {
temp[cnt++] = vec[j++];
}
}

for (int i = 0, j = l; i < cnt; i++, j++) {
vec[j] = temp[i];
}
}

void merge_sort(vector<int> &vec) {
vector<int> temp(vec.size());
ms(0, vec.size() - 1, vec, temp);
}

int main(int argc, char **argv) {
default_random_engine rd(time(NULL));

for (int cas = 0; cas < 100; cas++) {
vector<int> vec;
for (int i = 0; i < 100000; i++) {
vec.push_back(rd());
}
auto stl = vec;
merge_sort(vec);
sort(stl.begin(), stl.end());
assert(vec == stl);
}
return 0;
}