I can't seem to figure out what's wrong with my code. As the title says, I am trying to implement merge sort using integer vectors.
HEADER:
using Val = int;
using Container = std::vector<Val>;
using Iter = long;
void mergeSort(Container& arr, Iter start, Iter end);
.CPP (I've included the definition of merge in the file, just not pictured here)
void mergeSort(Container& arr, Iter start, Iter end) {
if (start >= end) return;
int mid = (start + end) / 2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, end, mid);
}
void merge(Container& arr, Iter start, Iter end, Iter mid)
{
int len = end - start + 1;
int x = 0;
Container temp;
temp.resize(arr.size());
int i, j, k;
i = start;
k = start;
j = mid + 1;
while (i <= mid && j <= end)
{
if (arr[i] < arr[j])
{
temp[k++] = arr[i++];
}
else
{
temp[k++] = arr[j++];
}
}
while (i <= mid) temp[k++] = arr[i++];
while (j <= end) temp[k++] = arr[j++];
for (k = 0; k < len; k++) arr[k + start] = temp[k];
}
many thanks!