-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathMergeSort.cpp
73 lines (57 loc) · 1.53 KB
/
MergeSort.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
66
67
68
69
70
71
72
73
/************************************************************************************
Merge sort. O(NlogN).
Based on problem 766 from informatics.mccme.ru:
http://informatics.mccme.ru/mod/statements/view3.php?id=1129&chapterid=766
************************************************************************************/
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>
using namespace std;
const int MAXN = 105000;
int n;
int a[MAXN], tmp[MAXN];
void mergesort(int l, int r) {
if (l >= r)
return;
int mid = (l + r) / 2;
mergesort(l, mid);
mergesort(mid + 1, r);
int p1 = l, p2 = mid + 1, cur = 1;
while (p1 <= mid || p2 <= r) {
if (p1 > mid || (p2 <= r && a[p2] < a[p1])) {
tmp[cur] = a[p2];
p2++;
}
else {
tmp[cur] = a[p1];
p1++;
}
cur++;
}
for (int i = 1; i <= r - l + 1; i++)
a[l + i - 1] = tmp[i];
}
int main() {
//assert(freopen("input.txt","r",stdin));
//assert(freopen("output.txt","w",stdout));
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
mergesort(1, n);
for (int i = 1; i <= n; i++)
printf("%d ", a[i]);
return 0;
}