-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmergesort.cpp
62 lines (53 loc) · 1.7 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
#include "mergesort.h"
using namespace std;
// MARK: Method definitions
void mergesort(vector<int>& vec) {
vector<int> helper(vec.size(), 0);
mergesort(vec, helper, 0, vec.size() - 1);
}
void mergesort(vector<int>& vec, vector<int>& helper, int low, int high) {
if (low < high) {
int mid = (low + high) / 2; // get midpoint
mergesort(vec, helper, low, mid); // sort left half
mergesort(vec, helper, mid + 1, high); // sort right half
merge(vec, helper, low, mid, high); // merge them
}
}
void merge(vector<int>& array, vector<int>& helper, int low, int mid, int high) {
// copy both halves into a helper array
for (int i = low; i <= high; ++i) {
helper[i] = array[i];
}
int helperLeft = low;
int helperRight = mid + 1;
int current = low;
// iterate through helper array
// compare left and right half
// copy smaller element from the two halves into the original array
while(helperLeft <= mid && helperRight <= high) {
// if left < right
if (helper[helperLeft] <= helper[helperRight]) {
array[current] = helper[helperLeft];
++helperLeft;
} else {
// if right < left
array[current] = helper[helperRight];
++helperRight;
}
++current;
}
// copy the rest of the left side of the array into the target array
int remaining = mid - helperLeft;
for (int i = 0; i <= remaining; ++i) {
array[current + i] = helper[helperLeft + i];
}
}
/*
int main() {
vector<int> test = {0, -3, 7, 6, 6, 5, 10};
mergesort(test);
for (auto& i: test) {
cout << i << endl;
}
}
*/