Merge sort algorithm not working correctly
<p>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.</p>

<p>Here is my code:</p>

<pre><code>#include&lt;iostream&gt;
#include&lt;stdlib.h&gt;

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 &lt; SIZE; k++)
    {
        arr[k] = rand() % 100;
        cout &lt;&lt; arr[k] &lt;&lt; " ";
    }

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

    cout &lt;&lt; endl;

    merge_sort(&amp;arg);

    cout &lt;&lt; "Sorted array: \n";

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

    return 0;
}

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

}

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

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

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

    }

    for(int k = arg-&gt;lowIndex; k &lt;= arg-&gt;highIndex; k++)
    {
        arg-&gt;numArray[k] = arg-&gt;tempArray[k];
    }
}
</code></pre>

<p>Here is the output I am getting:</p>

<pre><code>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
</code></pre>

<p>Can anyone point out what exactly I am doing wrong?</p>
