-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesort.cpp
64 lines (56 loc) · 1.51 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
#include <iostream>
#include <algorithm>
#include <vector>
void print(int const*const a, int n)
{
int i = 0;
while(i < n){
std::cout << a[i] << ",";
i++;
}
std::cout << "\ndone\n";
}
// uses twice the space to implement
std::vector<int> merge(std::vector<int> left, std::vector<int> right) {
std::vector<int> r;
// push values onto result vector
while (!left.empty() && !right.empty()) {
auto &t = (left.front() <= right.front()) ? left : right;
r.push_back(*t.begin());
t.erase(t.begin());
}
// now handle extra elements (vectors not same size)
while (!left.empty()) {
r.push_back(left.front());
left.erase(left.begin());
}
while (!right.empty()) {
r.push_back(right.front());
right.erase(right.begin());
}
return r;
}
// 1) A list of lenth one is sorted
std::vector<int> mergesort(std::vector<int> const& list) {
// base case, length 1 means sorted
if (list.size() <= 1) {
return list;
}
// sort two halves
std::vector<int> a, b;
auto const half = list.size() / 2;
for(auto i = 0u; i < list.size(); ++i) {
auto &v = (i < half) ? a : b;
v.push_back(list[i]);
}
auto r = mergesort(a);
auto r2 = mergesort(b);
return merge(r, r2);
}
int main() {
std::vector<int> const list = {3, 4, 6, 7, 2, 45, 4, 233, 12, 33, 17};
print(list.data(), list.size());
auto const l2 = mergesort(list);
print(l2.data(), l2.size());
return 0;
}