Mr. Johnny Doe
Mr. Johnny Doe

Reputation: 99

Recursion in Merge Sort not growing:

in my main function I have called (Coding done in C):

merge_sort(array,0,numberofelemts);

My actual sorting function looks like this

void merge_sort(int *array, int l, int r)
{

    while(r>l+1)
    {
        int mid=floor((l+r)/2);
        merge_sort(array, l, mid);
        merge_sort(array, mid, r);
        sort(array, l,mid, r);
    }

}

My issue is that the values get brought down to l=0 m=1 r=2 and then keeps looping over those values. From what I understand, after decreasing values to the right. It decreases the left and then grows the problem back towards its orginal size. Except the recursion seems to fail.

Upvotes: 0

Views: 32

Answers (1)

Goozzi
Goozzi

Reputation: 209

l and r are never modified, whenever u call this function with r > l + 1 it will never end.

if you want to implement merge_sort recursively, you probably want something something like :

if(r > l+1){
    int mid ....
    ....
}

Upvotes: 1

Related Questions