我想知道它是如何工作的

时间:2018-08-07 10:15:40

标签: c algorithm sorting mergesort

我想知道在此函数调用本身之后会发生什么,它将恢复运行,还是会继续调用自身直到条件变为假

void mergeSort(int arr[], int l, int r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);

        merge(arr, l, m, r);
    }
}

1 个答案:

答案 0 :(得分:0)

  

它将一直自称直到条件变为假

是的,就是这样。

该算法将范围从lr并将其分为两个范围,并在每个范围上调用mergesort

示例(为简短起见,我使用ms而不是mergeSort)

一系列初始通话:

ms(a, 0, 8) ---> ms(a, 0, 4) ---> ms(a, 0, 2) ---> ms(a, 0, 1) ---> ms(a, 0, 0)

由于值00未能满足条件l < r,因此该调用将仅返回,并且将执行先前递归级别中的mergeSort的第二次调用。导致:

ms(a, 0, 8) ---> ms(a, 0, 4) ---> ms(a, 0, 2) ---> ms(a, 0, 1) ---> ms(a, 0, 0)
                                                                |
                                                                --> ms(a, 1, 1) 

再次,值11失败了条件l < r,以便调用将仅返回,并且在merge之后,我们将返回到先前的递归级别并执行在该级别上第二次调用mergeSort

ms(a, 0, 8) ---> ms(a, 0, 4) ---> ms(a, 0, 2) ---> ms(a, 0, 1) ---> ms(a, 0, 0)
                                               |                |
                                               |                --> ms(a, 1, 1) 
                                               | 
                                               --> ms(a, 2, 2)

22仅使我们返回到先前的递归级别,其中第二次调用mergeSort。我想你现在看到图片了...

ms(a, 0, 8) ---> ms(a, 0, 4) ---> ms(a, 0, 2) ---> ms(a, 0, 1) ---> ms(a, 0, 0)
             |                |                |                |
             |                |                |                --> ms(a, 1, 1) 
             |                |                | 
             |                |                --> ms(a, 2, 2)
             |                |
             |                --> ms(a, 3, 4) ---> ms(a, 3, 3)
             |                                 |
             |                                 --> ms(a, 4, 4)
             |
             --> ms(a, 5, 8) ---> ... finish this your self ...

了解调用顺序的另一种方法是添加“递归级别”并在调用mergeSort时进行一些简单的打印:

#include <stdio.h>

void mergeSort(int arr[], int l, int r, int level)
{
  printf("Recursion level %d l=%d r=%d\n", level, l, r);
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m, level+1);
        mergeSort(arr, m+1, r, level+1);

        // merge commented out as it is irrelevant for the call sequence
        // merge(arr, l, m, r);
    }
}

int main()
{
  int a[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
  mergeSort(a, 0, 8, 0);
  return 0;
}

输出:

Recursion level 0 l=0 r=8
Recursion level 1 l=0 r=4
Recursion level 2 l=0 r=2
Recursion level 3 l=0 r=1
Recursion level 4 l=0 r=0
Recursion level 4 l=1 r=1
Recursion level 3 l=2 r=2
Recursion level 2 l=3 r=4
Recursion level 3 l=3 r=3
Recursion level 3 l=4 r=4
Recursion level 1 l=5 r=8
Recursion level 2 l=5 r=6
Recursion level 3 l=5 r=5
Recursion level 3 l=6 r=6
Recursion level 2 l=7 r=8
Recursion level 3 l=7 r=7
Recursion level 3 l=8 r=8

相同的输出,但具有取决于级别的缩进:

Recursion level 0 l=0 r=8
  Recursion level 1 l=0 r=4
    Recursion level 2 l=0 r=2
      Recursion level 3 l=0 r=1
        Recursion level 4 l=0 r=0
        Recursion level 4 l=1 r=1
      Recursion level 3 l=2 r=2
    Recursion level 2 l=3 r=4
      Recursion level 3 l=3 r=3
      Recursion level 3 l=4 r=4
  Recursion level 1 l=5 r=8
    Recursion level 2 l=5 r=6
      Recursion level 3 l=5 r=5
      Recursion level 3 l=6 r=6
    Recursion level 2 l=7 r=8
      Recursion level 3 l=7 r=7
      Recursion level 3 l=8 r=8