0

I have a problem. I started to learn how merge sort works, and now I'm stuck. I don't understand second recursion in merge function.

int merge_sort(int input[], int p, int r)
{
      if ( p >= r ) return 0; 

        int mid = floor((p + r) / 2);
        merge_sort(input, p, mid);
        **merge_sort(input, mid + 1, r);** 
        merge(input, p, r);
}

When first recursion is finished, I end up with something like this: Original array: 3 4 5 6 7 8

3 4 5 | 6 7 8

3 4 | 5

3 | 4

3

And then second recursion starts. So, my question is: does second recursion starts from array with only one element ( array with only one number, 3 in this case) or from original array ( 6 element array from beginning)? I hope you will understand what I mean. Thanks.

4

1 回答 1

0

我无法从您的代码中分辨出来,因为您总是在传递输入数组。但是是的,这个想法是分而治之,所以第一个电话应该是

合并排序:3 4 5 6 7 8

第一次递归:3 4 5

第二次递归:6 7 8

看看这张维基百科图片:http ://en.wikipedia.org/wiki/File:Merge_sort_algorithm_diagram.svg

于 2013-04-24T14:08:18.787 回答