0

Here is the implement for the Merge Sort function in my ADT. I am able to get Bubble Sort and Quick Sort working, but I get a Heap Corruption Error.

```
template<class Type>
void listType<Type>::mergeSort()
{
recursiveMergeSort(0, (maxSize - 1));
}//end of "mergeSort"
template<class Type>
void listType<Type>::recursiveMergeSort(int first, int last)
{
int middle = 0;
if(first < last)
{
middle = ((first + last) / 2);
recursiveMergeSort(first, middle);
recursiveMergeSort((middle + 1), last);
}
mergeList(first, middle, (middle + 1), last);
}//end of "recursiveMergeSort"
template<class Type>
void listType<Type>::mergeList(int first1, int last1, int first2, int last2)
{
int firstSublistIndex = first1; // set index for the 1st sublist
int secondSublistIndex = first2; // set index for the 2nd sublist
int bufferIndex = first1;
Type *buffer;
buffer = new Type[maxSize];
while ((firstSublistIndex <= last1) && (secondSublistIndex <= last2))
{
if (list[firstSublistIndex] < list[secondSublistIndex])
{
buffer[bufferIndex] = list[firstSublistIndex];
++firstSublistIndex;
}
else
{
buffer[bufferIndex] = list[secondSublistIndex];
++secondSublistIndex;
}
++bufferIndex;
};
while (firstSublistIndex <= last1)
{
buffer[bufferIndex] = list[firstSublistIndex];
++bufferIndex;
++firstSublistIndex;
};
while (secondSublistIndex <= last2)
{
buffer[bufferIndex] = list[secondSublistIndex];
++bufferIndex;
++secondSublistIndex;
};
for (bufferIndex = first1; bufferIndex <= last2 ; ++bufferIndex)
{
list[bufferIndex] = buffer[bufferIndex];
}
delete [] buffer;
} // end mergeList
```

*Edited 3 Years Ago by Dani*: Fixed formatting