Hi, I'm doing an assignment where I am supposed to implement a merge sort algorithm (shown below). Even though everything compiles, the elements are not actually sorted. I have been looking around for about an hour now and can't seem to figure out what is wrong with my code. Any input is greatly appreciated!
void mergesort(vector<int> &data) {
// implement me
vector<int> left;
vector<int> right;
if (data.size() <= 1) {
return;
}
else {
int mid = (data.size()/2);
for (int i = 0; i < mid; ++i)
left.push_back(data[i]);
for (unsigned int i = mid; i < data.size(); i++)
right.push_back(data[i]);
mergesort(left);
mergesort(right);
merge(left, right);
}
}
vector<int> merge(vector<int> &left, 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++;
}
return result;
// implement me
}