Just to be clear, this is for a homework assignment. I'm not asking for anyone to do it for me, I'm just stuck on a small portion of it.
I'm asked to implement mergesort, but each new array I make has to be placed in a stack of pointers. In the code below, I'm trying to split up an array recursively, then merge them together. My stack is named ptrs. The merge() function takes two sorted arrays and their sizes.
template <typename T>
T* MergeSort<T>::mergeSort(T arr[], int size) {
int size1 = (int)size/2;
int size2 = size - size1;
//I'll have a base case to cover the arrays of size 1 or 2
ptrs.push(new T[size1]);
for(int i = 0; i < size1; i++) {
ptrs.top()[i] = arr[i];
}
ptrs.push(new T[size2]);
for(int i = 0; i < size2; i++) {
ptrs.top()[i] = arr[i + size1];
}
return merge(mergeSort(TODO, size1), mergeSort(ptrs.top(), size2), size1, size2);
My problem is marked by my TODO. How can I access the first array, if the second one is now on the top of the stack?