merge sort issue?

I am new to C++, and am writing a merge sort program. However, every time I run it, the array is printed out for the second time, but unsorted. I think somehow that it isn't being returned. I am an early intermediate of java, and a similar program to this works fine, leading me to think that it's a syntactical error, not a logic error. Here's the code:

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 int * merge(int * left, int * right, int leftLength, int rightLength)
    {
        int * temp = new int[leftLength + rightLength];
        int x = 0;
        int y = 0;
        int index = 0;

        while (x < leftLength && y < rightLength)
         {
            if (left[x] <= right[y])
            {
                temp[index] = left[x];
                x++;
            }
            else
            {
                temp[index] = right[y];
                y++;
            }
           index++;
         }

        for (int j = x; j < leftLength; j++)
        {
            temp[index] = left[j];
            index++;
        }
        for (int j = y; j < rightLength; j++)
        {
            temp[index] = right[j];
            index++;
        }

        return temp;
     }

int * mergeSort(int * arr)
    {
        int arrLength = sizeof(arr)/sizeof(int);
        if (arrLength < 2)
            return arr;
        else
        {
            int mid = arrLength / 2;
            int * left = new int[mid];
            int * right = new int[arrLength-mid];
            int leftLength = sizeof(left)/sizeof(int);
            int rightLength = sizeof(right)/sizeof(int);

            for (int m = 0; m < leftLength; m++)
                left[m] = arr[m];



            for (int m = 0; m < rightLength; m++)
                right[m] = arr[mid + m];



            left = mergeSort(left);
            right = mergeSort(right);
            arr = merge(left, right, leftLength, rightLength);
            return arr;
        }
    }


int main()
{
    // below line necessary for random generator
    srand (time(NULL));
    int arr [50];
    int arrLength = (sizeof(arr)/sizeof(int));

    generate(arr, arrLength);

    for (int k = 0; k < arrLength; k++)
       cout << arr[k] << " ";
    cout << "\nIn order?\n";


    mergeSort(arr);

   for (int k = 0; k < arrLength; k++)
       cout << arr[k] << " ";
    cout << "Time spent " << finishTime;

    return 0;
}


I think it might be an issue with pointers, but I couldn't find out what. Thank you for any help you can give!
Last edited on
1
2
3
int * mergeSort(int * arr)
    {
        int arrLength = sizeof(arr)/sizeof(int);
this translates to sizeof(int *)/sizeof(int) (a constant). You should pass the size of the array to your function.

left = mergeSort(left); memory leak. You should delete the allocated memory before you loose the reference.
arr = merge(left, right, leftLength, rightLength); That will do nothing. You passed the pointer by value, but if you do it by reference it will throw a compiler error (because an array is a const pointer).
1
2
3
4
5
6
7
int array[10];
//this is what your function is doing
int *ptr = array;
ptr = some_place_with_sorted_data; //it does not affect array

//this is illegal
array = some_place_with_sorted_data;
Thank you for your help!
Topic archived. No new replies allowed.