-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
43 lines (36 loc) · 981 Bytes
/
Copy pathMergeSort.cpp
File metadata and controls
43 lines (36 loc) · 981 Bytes
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
#include <iostream>
using namespace std;
int n, v[100005], u[100005];
inline void merge(int *A, int *B, int const &st, int const &mid, int const &dr) {
int i = st, j = mid + 1, k = st - 1;
while (i <= mid && j <= dr) {
if (A[i] < A[j]) {
B[++k] = A[i++];
} else {
B[++k] = A[j++];
}
}
for (; i <= mid; i++) B[++k] = A[i];
for (; j <= dr; j++) B[++k] = A[j];
for (i = st; i <= dr; i++) A[i] = B[i];
}
inline void mergeSort(int *A, int *B, int const &st, int const &dr) {
if (st < dr) {
int mid = (st + dr) >> 1;
mergeSort(A, B, st, mid);
mergeSort(A, B, mid + 1, dr);
merge(A, B, st, mid, dr);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++)
cin >> v[i];
mergeSort(v, u, 0, n - 1);
for (int i = 0; i < n; i++)
cout << v[i] << ' ';
cout << '\n';
return 0;
}