CLRS Merge Sort Help

I'm trying to implement the merge sort algorithm explained in Intro to Algorithm, but I am unable to produce the correct result. Can anyone look at the code and point out where the problem is? I must have done something wrong in the merge() function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>

void merge( int arr[] , int low , int mid , int high )
{
    int n1 = mid - low + 1;
    int n2 = high - mid;

    int leftSubArray[n1 + 1];
    int rightSubArray[n2 + 1];

    for( int i = 0 ; i < n1 ; i++ ) {
        leftSubArray[i] = arr[low + i];
    }

    for( int i = 0 ; i < n2 ; i++ ) {
        rightSubArray[i] = arr[mid + i + 1];
    }

    // Sentinel - Has to be larger than any element in the array
    leftSubArray[n1] = 1000000;
    rightSubArray[n2] = 1000000;

    int i = 0;
    int j = 0;

    for( int k = low ; k < high ; k++ ) {
        if( leftSubArray[i] <= rightSubArray[j] ) {
            arr[k] = leftSubArray[i];
            i++;
        }
        else {
            arr[k] = rightSubArray[j];
            j++;
        }
    }
}

void mergeSort( int arr[] , int low , int high )
{
    if( low < high ) {
        int mid = ( low + high ) / 2;

        mergeSort( arr , low , mid );
        mergeSort( arr , mid + 1 , high );
        merge( arr , low , mid , high );
    }
}

int main()
{
    int arr[7] = { 4 , 6 , 1 , 10 , 5 , 3 , 7 };
    mergeSort( arr , 0 , 6 );

    for( int i = 0 ; i < 7 ; i++ ) {
        std::cout << arr[i] << " ";
    }
}
Last edited on
Nevermind. I figured out my mistake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>

void merge( int arr[] , int low , int mid , int high )
{
    int n1 = mid - low + 1;
    int n2 = high - mid;

    int leftSubArray[n1 + 1];
    int rightSubArray[n2 + 1];

    for( int i = 0 ; i < n1 ; i++ ) {
        leftSubArray[i] = arr[low + i];
    }

    for( int i = 0 ; i < n2 ; i++ ) {
        rightSubArray[i] = arr[mid + i + 1];
    }

    // Sentinel - Has to be larger than any element in the array
    leftSubArray[n1] = 1000000;
    rightSubArray[n2] = 1000000;

    int i = 0;
    int j = 0;

    for( int k = low ; k <= high ; k++ ) {
        if( leftSubArray[i] <= rightSubArray[j] ) {
            arr[k] = leftSubArray[i];
            i++;
        }
        else {
            arr[k] = rightSubArray[j];
            j++;
        }
    }
}

void mergeSort( int arr[] , int low , int high )
{
    if( low < high ) {
        int mid = ( low + high ) / 2;

        mergeSort( arr , low , mid );
        mergeSort( arr , mid + 1 , high );
        merge( arr , low , mid , high );
    }
}

int main()
{
    int arr[6] = { 4 , 6 , 1 , 5 , 3 , 7 };
    mergeSort( arr , 0 , 5 );

    for( int i = 0 ; i < 6 ; i++ ) {
        std::cout << arr[i] << " ";
    }
}
Topic archived. No new replies allowed.