forked from r0drigopaes/paa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.cpp
46 lines (35 loc) · 834 Bytes
/
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
#include <bits/stdc++.h>
using namespace std;
vector<int> merge_sort(const vector<int> &data)
{
if (data.size() <= 1) {
return data;
}
int middle = data.size() / 2;
vector<int> left(data.begin(), data.begin()+middle);
vector<int> right(data.begin()+middle, data.end());
left = merge_sort(left);
right = merge_sort(right);
vector<int> result(data.size());
merge(left.begin(), left.end(),
right.begin(), right.end(),
result.begin());
return result;
}
int main()
{
int n, x;
vector<int> a;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
scanf("%d",&x);
a.push_back(x);
}
vector<int> result = merge_sort(a);
for (int i = 0; i < result.size(); ++i)
{
printf("%d\n", result[i]);
}
return 0;
}