I'm currently reading CLRS while also trying to learn C++. As you can imagine everything is going great! I've been stuck on understanding MergeSort but over the last couple days have made good progress and feel like my code is almost there. P.S. Java is my strong suit so if I mix up the vocab between languages I apologize!
I imagine the problem is stemming from one of the few issues below:
1)Because I'm still new to C++ I've read that during instances it's important to use the class variables in the way I have below in order to apply the size of the array throughout your code. With that being said, I think during my recursion the fact that my r variable is a read-only constant; it is making my code go through an extra iteration of the recursion which is leading to problems.
-My solution to this was to just set that variable equal to a new one and have it adapt to the array as needed. This still lead to wrong solutions!
2)I've read numerous times to not use Arrays in C++ especially in situations such as this and to use vectors instead. Given that I haven't learned about vectors quite yet, I felt more comfortable using Arrays and believe an issue may be from incorrect coding of pointers and references in my program.
3)My final assumption also comes from reading a lot of answers to similar questions which is to not use instance variables in recursive methods. I'm pretty sure this wasn't a problem in Java and felt like this may just be a big no-no in C++ specifically.
I've seen countless classes of MergeSort and am very confident that my mergeSort & merge methods are perfect which makes me believe that this problem is probably just a lack of knowledge to C++ syntax or rules! Any comments or help would be greatly appreciated! Lastly 2 results of arrays I got quite often were [2, 4, 4, 5, 4, 4, 5, 1] & [2, 4, 5, 4, 5, 7, 1]
#include <iostream>
#include <cmath>
using namespace std;
class MergeSort
{
static const size_t r = 8;
int A[r];
public:
MergeSort() : A{2, 4, 5, 7, 1, 2, 3, 6}
{
printMergeSort(A, r);
int p = 0;
mergeSort(A, p, r);
printMergeSort(A, r);
}
static void mergeSort(int *A, int p, int r)
{
if(p < r)
{
//int q = floor(((p + r) / 2));
int q = ((p + r) / 2);
mergeSort(A, p, q);
mergeSort(A, q + 1, r);
merge(A, p, q, r);
}
}
static void merge(int *A, int q, int p, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
int L[n1+1];
int R[n2+1];
for(int i = 0; i < n1; i++)
{
L[i] = A[p + i];
}
for(int j = 0; j < n2; j++)
{
R[j] = A[q + j + 1];
}
//L[n1 + 1] = std::numeric_limits<int>::max();
L[n1] = INT_MAX;
R[n2] = INT_MAX;
int i = 0;
int j = 0;
for(int k = p; k <= r; k++)
{
if(L[i] <= R[j])
{
A[k] = L[i];
i = i + 1;
}
else
{
A[k] = R[j];
j = j + 1;
}
}
}
static void printMergeSort(int *A, size_t r)
{
for(int i = 0; i < r; i++)
{
cout << A[i] << " ";
}
cout << endl;
}
};
int main()
{
//MergeSort();
MergeSort x;
return 0;
}