Question

(C++) Hi all, this is my first attempt at mergesort. I see my code similar to someone who has posted an earlier question: here. At the lower levels, sorting works as expected. However, I believe that my issue here is the sorted array goes out of scope after each lower level, therefore never properly sorting. By that case, I tried passing by reference using "std::vector&". Am I wrong? What is the proper way to pass by reference?

Below is my code:

/*
MERGE SORT
breaking down the sorting into pairs concursively until we are comparing only two values
The two integers parameters denotes indices for beginning and ending values in vector
Merge sort inclusive of both left and right range
*/
#include <vector> // for using vectors in c++
void merge(std::vector<int>& inputv, int beg_i, int end_i)
{
    int d = beg_i;  // used as an index
    int e = beg_i + (end_i - beg_i)/2 + 1;  // used as an index

    int y = beg_i + (end_i - beg_i)/2;     // used as a check against d
    int z = end_i;  // used as a check against e

    std::vector<int> tempvect(inputv.size());

    for (int c = beg_i; c < z+1; c++)
    {
        if (e > z && d > y)
        {
            // do nothing
        }
        else if (e > z)
        {
            tempvect[c] = inputv[d];
            d++;
        }
        else if (d > y)
        {
            tempvect[c] = inputv[e];
            e++;
        }

        else if(inputv[d] > inputv[e])
        {
            tempvect[c] = inputv[e];
            e++;
        }
        else if(inputv[d] <= inputv[e])
        {
            tempvect[c] = inputv[d];
            d++;
        }
    }
    for (int i = beg_i; i < end_i+1; i++)
    {
        inputv[i] = tempvect[i];
    }
}

void mergesort(std::vector<int> inputvector, int beg_i, int end_i)
{
    if(end_i - beg_i == 0) 
    {
        // do nothing
    }
    if(end_i - beg_i >= 1)
    {
        int mid_i = beg_i + (end_i - beg_i)/2;

        mergesort(inputvector,beg_i,mid_i); // reclusive to last card
        mergesort(inputvector,mid_i+1,end_i);   // resulsive to last card
        merge(inputvector,beg_i,end_i); // actual implementation to sort and merge the vectors
    }

}
Was it helpful?

Solution

If you want to mimic the code you saw in the thread you highlighted, note that the code uses a pointer. The function that uses the pointer is basically changing what the pointer is pointing to.

Therefore you should do this to simulate this behavior:

void mergesort(std::vector<int>& inputvector, int beg_i, int end_i)

You must pass the vector by reference. The reason why you did not see the results is that passing by value creates a local temporary copy, and when the function returns, the temporary goes away. You don't want this -- you want the results to be directed to the vector you're passing in, not a temporary copy, so pass the vector by reference.

OTHER TIPS

Anyway, I realised that the reason why it still goes out of scope is although I did this:

void merge(std::vector<int>& inputv, int beg_i, int end_i)

I failed to do this:

void mergesort(std::vector<int>& inputvector, int beg_i, int end_i)

Thanks to PaulMcKenzie for the prompt reply, that pointed out my silly folly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top