-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathwcw.cpp
71 lines (51 loc) · 1.18 KB
/
wcw.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
#include <iostream>
#include <vector>
using namespace std;
vector<int> merge(vector<int>& vec, const vector<int>& left, const vector<int>& right) {
vector<int> result;
unsigned left_it = 0, right_it = 0;
while (left_it < left.size() && right_it < right.size()) {
if (left[left_it] < right[right_it]) {
result.push_back(left[left_it]);
left_it++;
} else {
result.push_back(right[right_it]);
right_it++;
}
}
while (left_it < left.size()) {
result.push_back(left[left_it]);
left_it++;
}
while (right_it < right.size()) {
result.push_back(right[right_it]);
right_it++;
}
vec = result;
return vec;
}
vector<int> merge_sort(vector<int>& vec) {
if (vec.size() == 1)
return vec;
vector<int>::iterator middle = vec.begin() + (vec.size() / 2);
vector<int> left(vec.begin(), middle);
vector<int> right(middle, vec.end());
left = merge_sort(left);
right = merge_sort(right);
return merge(vec, left, right);
}
int main() {
int n;
cin >> n;
while (n--) {
int x, temp;
cin >> x;
vector<int> v;
while (x--) {
cin >> temp;
v.push_back(temp);
}
cout << merge_sort(v) << endl;
}
return 0;
}