1

As part of a homework assignment, I need to be able to implement a merge sort using a structure as the main argument. Having not been familiar with the merge sort until today, i have attempted to write my own implementation of it. For some reason I cannot get it to work.

Here is my code:

#include<iostream>
#include<stdlib.h>

using namespace std;

struct MergeArgument
{
    int *numArray;
    int *tempArray;
    int lowIndex, highIndex;
};

void merge(MergeArgument*);
void merge_sort(MergeArgument*);

int main(int argc, char** argv)
{   int SIZE = 25;
    MergeArgument arg;
    int arr[SIZE];
    int temp[SIZE];

    for(int k = 0; k < SIZE; k++)
    {
        arr[k] = rand() % 100;
        cout << arr[k] << " ";
    }

    arg.numArray = arr;
    arg.tempArray = temp;
    arg.lowIndex = 0;
    arg.highIndex = SIZE - 1;

    cout << endl;

    merge_sort(&arg);

    cout << "Sorted array: \n";

    for (int i = 0; i < SIZE; i++)
        cout << arr[i] << " ";
    cout << endl;

    return 0;
}

void merge_sort(MergeArgument *arg)
{   int tempHigh, tempLow;
    if(arg->lowIndex < arg->highIndex)
    {
        tempHigh = arg->highIndex;
        tempLow = arg->lowIndex;
        arg->highIndex = (tempHigh + tempLow) / 2;
        merge_sort(arg);
        arg->highIndex = tempHigh;
        arg->lowIndex = ((tempHigh + tempLow) / 2) + 1;
        merge_sort(arg);
        arg->lowIndex = tempLow;
        merge(arg);
    }

}

void merge(MergeArgument *arg)
{   int low = arg->lowIndex, mid = ((arg->lowIndex + arg->highIndex) / 2), high = arg->highIndex;
    int i = low, lowCounter = low, highCounter = mid + 1;

    while((lowCounter <= mid) && (highCounter <= high))
    {
        if(arg->numArray[lowCounter] < arg->numArray[highCounter])
        {
            arg->tempArray[i] = arg->numArray[lowCounter];
            lowCounter++;
        }
        else
        {
            arg->tempArray[i] = arg->numArray[highCounter];
            highCounter++;
        }
        i++;
    }

    if (lowCounter < mid)
    {
        for (int k = lowCounter; k < mid; k++)
        {
            arg->tempArray[i] = arg->numArray[k];
            i++;
        }
    }
    else
    {
        for (int k = highCounter; k <= arg->highIndex; k++)
        {
            arg->tempArray[i] = arg->numArray[k];
            i++;
        }

    }

    for(int k = arg->lowIndex; k <= arg->highIndex; k++)
    {
        arg->numArray[k] = arg->tempArray[k];
    }
}

Here is the output I am getting:

83 86 77 15 93 35 86 92 49 21 62 27 90 59 63 26 40 26 72 36 11 68 67 29 82    
Sorted array:  
11 -1216235240 15 0 21 26 -1079135248 26 27 -1079135396 29 -1216770650 35 -1216235240 49 -1216492084 59 0 68 72 82 83 0 86 82

Can anyone point out what exactly I am doing wrong?

10
  • No need to copy tags into the title. Commented Oct 30, 2011 at 1:53
  • 3
    For a C++ program, this looks an awful lot like C to me. Commented Oct 30, 2011 at 1:55
  • This is very rough code, I simply want to get the merge sort working before I move on to the big part of the assignment. It will look like C++ by the time I am done with it. Commented Oct 30, 2011 at 2:00
  • I'm trying to figure out how you could make it work with only one instance of MergeArgument, and only one argument passed to merge(). Commented Oct 30, 2011 at 2:05
  • 1
    @michaelnett -- I'm not sure I'm seeing enough local variables. Commented Oct 30, 2011 at 3:07

1 Answer 1

1

This is pretty close to working, although you might want to consider some of the comments folks have made on making this more C++ like. No doubt this is from hard-won experience that there is never enough time to go back and do what you really should do.

The problem I see is here, in merge:

if (lowCounter < mid)
{
    for (int k = lowCounter; k < mid; k++)
    {
        arg->tempArray[i] = arg->numArray[k];
        i++;
    }
}

You might want to compare and contrast the bounds here to the initial loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Although I got it working by redoing my merge function, I am glad to finally see what was wrong with my original implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.