24

我看到的大多数合并排序实现都与此类似。算法书介绍以及我搜索的在线实现。我的递归印章并没有比弄乱斐波那契生成(这很简单)走得更远,所以也许是多次递归让我大吃一惊,但我什至无法单步执行代码并理解发生了什么,甚至在我击中之前合并功能。

它是如何一步步完成的?为了更好地理解这里的过程,我应该进行一些策略或阅读吗?

void mergesort(int *a, int*b, int low, int high)
{
    int pivot;
    if(low<high)
    {
        pivot=(low+high)/2;
        mergesort(a,b,low,pivot);
        mergesort(a,b,pivot+1,high);
        merge(a,b,low,pivot,high);
    }
}

和合并(尽管坦率地说,在我进入这部分之前,我已经精神崩溃了)

void merge(int *a, int *b, int low, int pivot, int high)
{
    int h,i,j,k;
    h=low;
    i=low;
    j=pivot+1;

    while((h<=pivot)&&(j<=high))
    {
        if(a[h]<=a[j])
        {
            b[i]=a[h];
            h++;
        }
        else
        {
            b[i]=a[j];
            j++;
        }
        i++;
    }
    if(h>pivot)
    {
        for(k=j; k<=high; k++)
        {
            b[i]=a[k];
            i++;
        }
    }
    else
    {
        for(k=h; k<=pivot; k++)
        {
            b[i]=a[k];
            i++;
        }
    }
    for(k=low; k<=high; k++) a[k]=b[k];
}
4

10 回答 10

22

合并排序:

1)将数组分成两半
2)对左半部分
排序 3)对右半部分排序
4)将两半合并在一起

在此处输入图像描述

在此处输入图像描述

于 2015-12-11T20:25:58.207 回答
17

我认为 MergeSort 中的“排序”函数名称有点用词不当,它确实应该称为“除法”。

这是正在处理的算法的可视化。

在此处输入图像描述

每次函数递归时,它都会对输入数组进行越来越小的细分,从它的左半部分开始。每次函数从递归返回时,它将继续并开始在右半部分工作,或者再次递归并在更大的一半上工作。

像这样

[************************]mergesort
[************]mergesort(lo,mid)
[******]mergesort(lo,mid)
[***]mergesort(lo,mid)
[**]mergesort(lo,mid)
 [**]mergesort(mid+1,hi)
[***]merge
   [***]mergesort(mid+1,hi)
   [**]mergesort*(lo,mid)
    [**]mergesort(mid+1,hi)
   [***]merge
[******]merge
      [******]mergesort(mid+1,hi)
      [***]mergesort(lo,mid)
      [**]mergesort(lo,mid)
       [**]mergesort(mid+1,hi)
      [***]merge
         [***]mergesort(mid+1,hi)
         [**]mergesort(lo,mid)
           [**]mergesort(mid+1,hi)
         [***]merge
      [******]merge
[************]merge
            [************]mergesort(mid+1,hi)
            [******]mergesort(lo,mid)
            [***]mergesort(lo,mid)
            [**]mergesort(lo,mid)
             [**]mergesort(mid+1,hi)
            [***]merge
               [***]mergesort(mid+1,hi)
               [**]mergesort(lo,mid)
                 [**]mergesort(mid+1,hi)
               [***]merge
            [******]merge
                  [******]mergesort(mid+1,hi)
                  [***]mergesort(lo,mid)
                  [**]mergesort*(lo,mid)
                    [**]mergesort(mid+1,hi)
                  [***]merge
                     [***]mergesort(mid+1,hi)    
                     [**]mergesort(lo,mid)
                      [**]mergesort(mid+1,hi)
                     [***]merge
                  [******]merge
            [************]merge
[************************]merge
于 2014-03-18T23:42:19.583 回答
9

一个显而易见的事情是在纸上尝试在一个小数组上进行这种合并排序,比如大小为 8(2 的幂在这里很方便)。假设你是一台执行代码的计算机,看看它是否开始变得更清晰一些。

你的问题有点模棱两可,因为你没有解释你觉得困惑的地方,但听起来你正试图在你的脑海中展开递归调用。这可能是一件好事,也可能不是一件好事,但我认为它很容易导致你一次想太多。与其试图从头到尾跟踪代码,不如看看你是否能抽象地理解这个概念。合并排序:

  1. 将数组分成两半
  2. 对左半部分进行排序
  3. 对右半部分进行排序
  4. 将两半合并在一起

(1) 对您来说应该是相当明显和直观的。对于步骤 (2) 的关键见解是,数组的左半部分......是一个数组。假设您的合并排序有效,它应该能够对数组的左半部分进行排序。对?步骤(4)实际上是算法的一个非常直观的部分。一个例子应该让它变得微不足道:

at the start
left: [1, 3, 5], right: [2, 4, 6, 7], out: []

after step 1
left: [3, 5], right: [2, 4, 6, 7], out: [1]

after step 2
left: [3, 5], right: [4, 6, 7], out: [1, 2]

after step 3
left: [5], right: [4, 6, 7], out: [1, 2, 3]

after step 4
left: [5], right: [6, 7], out: [1, 2, 3, 4]

after step 5
left: [], right: [6, 7], out: [1, 2, 3, 4, 5]

after step 6
left: [], right: [7], out: [1, 2, 3, 4, 5, 6]

at the end
left: [], right: [], out: [1, 2, 3, 4, 5, 6, 7]

因此,假设您了解(1)和(4),另一种思考归并排序的方法就是这样。想象一下别人写mergesort()的,你确信它有效。然后您可以使用该实现mergesort()来编写:

sort(myArray)
{
    leftHalf = myArray.subArray(0, myArray.Length/2);
    rightHalf = myArray.subArray(myArray.Length/2 + 1, myArray.Length - 1);

    sortedLeftHalf = mergesort(leftHalf);
    sortedRightHalf = mergesort(rightHalf);

    sortedArray = merge(sortedLeftHalf, sortedRightHalf);
}

请注意,sort不使用递归。它只是说“对两半进行排序然后合并它们”。如果你理解了上面的合并示例,那么希望你能直观地看到这个sort函数似乎按照它所说的去做......排序。

现在,如果你更仔细地看……sort()看起来很像mergesort()!那是因为它是mergesort()(除了它没有基本情况,因为它不是递归的!)。

但这就是我喜欢考虑递归函数的方式——假设函数在你调用它时工作。把它当作一个黑匣子来做你需要它做的事情。当你做出这样的假设时,弄清楚如何填写那个黑匣子通常很容易。对于给定的输入,你能把它分解成更小的输入来输入你的黑匣子吗?解决这个问题后,剩下的唯一事情就是在函数开始时处理基本情况(在这种情况下,您不需要进行任何递归调用。例如,mergesort([])只返回一个空数组;它没有t 对 ) 进行递归调用mergesort()

最后,这有点抽象,但理解递归的一个好方法实际上是使用归纳来编写数学证明。用于编写归纳证明的相同策略用于编写递归函数:

数学证明:

  • 显示声明对于基本案例是正确的
  • 假设对于小于一些的输入是正确的n
  • 使用该假设来证明对于 size 的输入仍然是正确的n

递归函数:

  • 处理基本情况
  • 假设您的递归函数适用于小于一些的输入n
  • 使用该假设来处理大小的输入n
于 2013-09-29T00:15:35.227 回答
6

关于合并排序的递归部分,我发现这个页面非常有用。您可以在执行代码时关注它。它向您展示了首先执行的内容,以及接下来执行的内容。

汤姆

于 2015-04-11T13:02:12.830 回答
4

简单地将mergesort()数组分成两半,直到if条件失败,即low < high. 当您调用mergesort()两次时:一次使用lowto pivot,第二次使用pivot+1to high,这将进一步划分子数组。

让我们举个例子:

a[] = {9,7,2,5,6,3,4}
pivot = 0+6/2 (which will be 3)
=> first mergesort will recurse with array {9,7,2} : Left Array
=> second will pass the array {5,6,3,4} : Right Array

它将重复,直到每个元素leftright数组中都有 1 个元素。最后你会得到类似的东西:

L : {9} {7} {2} R : {5} {6} {3} {4} (each L and R will have further sub L and R)
=> which on call to merge will become

L(L{7,9} R{2}) : R(L{5,6} R{3,4})
As you can see that each sub array are getting sorted in the merge function.

=> on next call to merge the next L and R sub arrays will get in order
L{2,7,9} : R{3,4,5,6}

Now both L and R sub array are sorted within
On last call to merge they'll be merged in order

Final Array would be sorted => {2,3,4,5,6,7,9}

请参阅@roliu 给出的答案中的合并步骤

于 2013-09-29T15:52:03.260 回答
3

如果以这种方式回答,我深表歉意。我承认这只是一个草图,而不是深入的解释。

虽然看实际代码如何映射到递归并不明显,但我能够以这种方式从一般意义上理解递归。

以示例未排序集{2,9,7,5}作为输入。下面为简洁起见,merge_sort 算法用“ms”表示。然后我们可以将操作描述为:

第 1 步:毫秒(毫秒(毫秒(2),毫秒(9)),毫秒(毫秒(7),毫秒(5)))

第 2 步:毫秒(毫秒({2},{9}),毫秒({7},{5}))

第 3 步:毫秒({2,9},{5,7})

第 4 步:{2,5,7,9}

需要注意的是,单线态的 merge_sort(如{2})只是单线态(ms(2) = {2}),因此在最深层次的递归中,我们得到了第一个答案。当内部递归完成并合并在一起时,剩下的答案就会像多米诺骨牌一样翻滚。

该算法的部分天才之处在于它通过其构造自动构建步骤 1 的递归公式的方式。帮助我的是思考如何将上面的步骤 1 从静态公式转变为一般递归。

于 2015-12-28T23:12:50.687 回答
1

我知道这是一个老问题,但我想谈谈我的想法是什么帮助我理解了归并排序。

合并排序有两大部分

  1. 将数组拆分为更小的块(划分)
  2. 将数组合并在一起(征服)

递归的作用只是分割部分。

我认为让大多数人感到困惑的是,他们认为拆分和确定要拆分的内容有很多逻辑,但排序的大部分实际逻辑都发生在merge上。递归只是用于划分并执行前半部分,然后后半部分实际上只是循环,复制内容。

我看到一些提到枢轴的答案,但我建议不要将“枢轴”一词与合并排序相关联,因为这是将合并排序与快速排序混淆的简单方法(这在很大程度上依赖于选择“枢轴”)。它们都是“分而治之”的算法。对于合并排序,除法总是发生在中间,而对于快速排序,您可以在选择最佳枢轴时巧妙地使用除法。

于 2017-12-02T20:15:23.300 回答
1

尝试解决递归的每一步通常不是一个理想的方法,但对于初学者来说,它肯定有助于理解递归背后的基本思想,并且可以更好地编写递归函数。


这是合并排序的 C 解决方案:-

#include <stdio.h>
#include <stdlib.h>


void merge_sort(int *, unsigned);
void merge(int *, int *, int *, unsigned, unsigned);


int main(void)
{

    unsigned size;
    printf("Enter the no. of integers to be sorted: ");
    scanf("%u", &size);

    int * arr = (int *) malloc(size * sizeof(int));
    if (arr == NULL)
        exit(EXIT_FAILURE);

    printf("Enter %u integers: ", size);
    for (unsigned i = 0; i < size; i++)
        scanf("%d", &arr[i]);

    merge_sort(arr, size);

    printf("\nSorted array: ");
    for (unsigned i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");

    free(arr);

    return EXIT_SUCCESS;

}


void merge_sort(int * arr, unsigned size)
{

    if (size > 1)
    {
        unsigned left_size = size / 2;
        int * left = (int *) malloc(left_size * sizeof(int));
        if (left == NULL)
            exit(EXIT_FAILURE);
        for (unsigned i = 0; i < left_size; i++)
            left[i] = arr[i];

        unsigned right_size = size - left_size;
        int * right = (int *) malloc(right_size * sizeof(int));
        if (right == NULL)
            exit(EXIT_FAILURE);
        for (unsigned i = 0; i < right_size; i++)
            right[i] = arr[i + left_size];

        merge_sort(left, left_size);
        merge_sort(right, right_size);
        merge(arr, left, right, left_size, right_size);

        free(left);
        free(right);
    }

}


/*
   This merge() function takes a target array (arr) and two sorted arrays (left and right),
   all three of them allocated beforehand in some other function(s).
   It then merges the two sorted arrays (left and right) into a single sorted array (arr).
   It should be ensured that the size of arr is equal to the size of left plus the size of right.
*/

void merge(int * arr, int * left, int * right, unsigned left_size, unsigned right_size)
{

    unsigned i = 0, j = 0, k = 0;

    while ((i < left_size) && (j < right_size))
    {
        if (left[i] <= right[j])
            arr[k++] = left[i++];
        else
            arr[k++] = right[j++];
    }

    while (i < left_size)
        arr[k++] = left[i++];

    while (j < right_size)
        arr[k++] = right[j++];

}

这是递归的分步说明:-

   Let arr be [1,4,0,3,7,9,8], having the address 0x0000.

   In main(), merge_sort(arr, 7) is called, which is the same as merge_sort(0x0000, 7).
   After all of the recursions are completed, arr (0x0000) becomes [0,1,3,4,7,8,9].


                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
   arr - 0x0000 - [1,4,0,3,7,9,8]                   |                                                    |                                                    |
   size - 7                                         |                                                    |                                                    |
                                                    |                                                    |                                                    |
   left = malloc() - 0x1000a (say) - [1,4,0]        |                                                    |                                                    |
   left_size - 3                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
   right = malloc() - 0x1000b (say) - [3,7,9,8]     |                                                    |                                                    |
   right_size - 4                                   |                                                    |                                                    |
                                                    |                                                    |                                                    |
   merge_sort(left, left_size) -------------------> |   arr - 0x1000a - [1,4,0]                          |                                                    |
                                                    |   size - 3                                         |                                                    |
                                                    |                                                    |                                                    |
                                                    |   left = malloc() - 0x2000a (say) - [1]            |                                                    |
                                                    |   left_size = 1                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |   right = malloc() - 0x2000b (say) - [4,0]         |                                                    |
                                                    |   right_size = 2                                   |                                                    |
                                                    |                                                    |                                                    |
                                                    |   merge_sort(left, left_size) -------------------> |   arr - 0x2000a - [1]                              |
                                                    |                                                    |   size - 1                                         |
                                                    |   left - 0x2000a - [1] <-------------------------- |   (0x2000a has only 1 element)                     |
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |   merge_sort(right, right_size) -----------------> |   arr - 0x2000b - [4,0]                            |
                                                    |                                                    |   size - 2                                         |
                                                    |                                                    |                                                    |
                                                    |                                                    |   left = malloc() - 0x3000a (say) - [4]            |
                                                    |                                                    |   left_size = 1                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |   right = malloc() - 0x3000b (say) - [0]           |
                                                    |                                                    |   right_size = 1                                   |
                                                    |                                                    |                                                    |
                                                    |                                                    |   merge_sort(left, left_size) -------------------> |   arr - 0x3000a - [4]
                                                    |                                                    |                                                    |   size - 1
                                                    |                                                    |   left - 0x3000a - [4] <-------------------------- |   (0x3000a has only 1 element)
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |   merge_sort(right, right_size) -----------------> |   arr - 0x3000b - [0]
                                                    |                                                    |                                                    |   size - 1
                                                    |                                                    |   right - 0x3000b - [0] <------------------------- |   (0x3000b has only 1 element)
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |   merge(arr, left, right, left_size, right_size)   |
                                                    |                                                    |   i.e. merge(0x2000b, 0x3000a, 0x3000b, 1, 1)      |
                                                    |   right - 0x2000b - [0,4] <----------------------- |   (0x2000b is now sorted)                          |
                                                    |                                                    |                                                    |
                                                    |                                                    |   free(left) (0x3000a is now freed)                |
                                                    |                                                    |   free(right) (0x3000b is now freed)               |
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |   merge(arr, left, right, left_size, right_size)   |                                                    |
                                                    |   i.e.  merge(0x1000a, 0x2000a, 0x2000b, 1, 2)     |                                                    |
   left - 0x1000a - [0,1,4] <---------------------- |   (0x1000a is now sorted)                          |                                                    |
                                                    |                                                    |                                                    |
                                                    |   free(left) (0x2000a is now freed)                |                                                    |
                                                    |   free(right) (0x2000b is now freed)               |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
   merge_sort(right, right_size) -----------------> |   arr - 0x1000b - [3,7,9,8]                        |                                                    |
                                                    |   size - 4                                         |                                                    |
                                                    |                                                    |                                                    |
                                                    |   left = malloc() - 0x2000c (say) - [3,7]          |                                                    |
                                                    |   left_size = 2                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |   right = malloc() - 0x2000d (say) - [9,8]         |                                                    |
                                                    |   right_size = 2                                   |                                                    |
                                                    |                                                    |                                                    |
                                                    |   merge_sort(left, left_size) -------------------> |   arr - 0x2000c - [3,7]                            |
                                                    |                                                    |   size - 2                                         |
                                                    |                                                    |                                                    |
                                                    |                                                    |   left = malloc() - 0x3000c (say) - [3]            |
                                                    |                                                    |   left_size = 1                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |   right = malloc() - 0x3000d (say) - [7]           |
                                                    |                                                    |   right_size = 1                                   |
                                                    |                                                    |                                                    |
                                                    |                                                    |   merge_sort(left, left_size) -------------------> |   arr - 0x3000c - [3]
                                                    |        left - [3,7] was already sorted, but        |                                                    |   size - 1
                                                    |        that doesn't matter to this program.        |   left - 0x3000c - [3] <-------------------------- |   (0x3000c has only 1 element)
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |   merge_sort(right, right_size) -----------------> |   arr - 0x3000d - [7]
                                                    |                                                    |                                                    |   size - 1
                                                    |                                                    |   right - 0x3000d - [7] <------------------------- |   (0x3000d has only 1 element)
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |   merge(arr, left, right, left_size, right_size)   |
                                                    |                                                    |   i.e. merge(0x2000c, 0x3000c, 0x3000d, 1, 1)      |
                                                    |   left - 0x2000c - [3,7] <------------------------ |   (0x2000c is now sorted)                          |
                                                    |                                                    |                                                    |
                                                    |                                                    |   free(left) (0x3000c is now freed)                |
                                                    |                                                    |   free(right) (0x3000d is now freed)               |
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |   merge_sort(right, right_size) -----------------> |   arr - 0x2000d - [9,8]                            |
                                                    |                                                    |   size - 2                                         |
                                                    |                                                    |                                                    |
                                                    |                                                    |   left = malloc() - 0x3000e (say) - [9]            |
                                                    |                                                    |   left_size = 1                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |   right = malloc() - 0x3000f (say) - [8]           |
                                                    |                                                    |   right_size = 1                                   |
                                                    |                                                    |                                                    |
                                                    |                                                    |   merge_sort(left, left_size) -------------------> |   arr - 0x3000e - [9]
                                                    |                                                    |                                                    |   size - 1
                                                    |                                                    |   left - 0x3000e - [9] <-------------------------- |   (0x3000e has only 1 element)
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |   merge_sort(right, right_size) -----------------> |   arr - 0x3000f - [8]
                                                    |                                                    |                                                    |   size - 1
                                                    |                                                    |   right - 0x3000f - [8] <------------------------- |   (0x3000f has only 1 element)
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |   merge(arr, left, right, left_size, right_size)   |
                                                    |                                                    |   i.e. merge(0x2000d, 0x3000e, 0x3000f, 1, 1)      |
                                                    |   right - 0x2000d - [8,9] <----------------------- |   (0x2000d is now sorted)                          |
                                                    |                                                    |                                                    |
                                                    |                                                    |   free(left) (0x3000e is now freed)                |
                                                    |                                                    |   free(right) (0x3000f is now freed)               |
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |   merge(arr, left, right, left_size, right_size)   |                                                    |
                                                    |   i.e.  merge(0x1000b, 0x2000c, 0x2000d, 2, 2)     |                                                    |
   right - 0x1000b - [3,7,8,9] <------------------- |   (0x1000b is now sorted)                          |                                                    |
                                                    |                                                    |                                                    |
                                                    |   free(left) (0x2000c is now freed)                |                                                    |
                                                    |   free(right) (0x2000d is now freed)               |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
   merge(arr, left, right, left_size, right_size)   |                                                    |                                                    |
   i.e. merge(0x0000, 0x1000a, 0x1000b, 3, 4)       |                                                    |                                                    |
   (0x0000 is now sorted)                           |                                                    |                                                    |
                                                    |                                                    |                                                    |
   free(left) (0x1000a is now freed)                |                                                    |                                                    |
   free(right) (0x1000b is now freed)               |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |
                                                    |                                                    |                                                    |

于 2021-07-23T08:11:23.540 回答
0

将问题划分为子问题的过程 给出的示例将帮助您理解递归。int A[]={要短路的元素个数}, int p=0; (情人指数)。int r= A.length - 1;(更高的索引)。

class DivideConqure1 {
void devide(int A[], int p, int r) {
    if (p < r) {
        int q = (p + r) / 2; // divide problem into sub problems.
        devide(A, p, q);   //divide left problem into sub problems
        devide(A, q + 1, r); //divide right problem into sub problems
        merger(A, p, q, r);  //merger the sub problem
    }
}

void merger(int A[], int p, int q, int r) {
    int L[] = new int[q - p + 1];
    int R[] = new int[r - q + 0];

    int a1 = 0;
    int b1 = 0;
    for (int i = p; i <= q; i++) {  //store left sub problem in Left temp
        L[a1] = A[i];
        a1++;
    }
    for (int i = q + 1; i <= r; i++) { //store left sub problem in right temp
        R[b1] = A[i];
        b1++;
    }
    int a = 0;
    int b = 0; 
    int c = 0;
    for (int i = p; i < r; i++) {
        if (a < L.length && b < R.length) {
            c = i + 1;
            if (L[a] <= R[b]) { //compare left element<= right element
                A[i] = L[a];
                a++;
            } else {
                A[i] = R[b];
                b++;
            }
        }
    }
    if (a < L.length)
        for (int i = a; i < L.length; i++) {
            A[c] = L[i];  //store remaining element in Left temp into main problem 
            c++;
        }
    if (b < R.length)
        for (int i = b; i < R.length; i++) {
            A[c] = R[i];  //store remaining element in right temp into main problem 
            c++;
        }
}
于 2017-08-15T05:06:21.967 回答
0

当您调用递归方法时,它不会在堆栈到堆栈内存的同时执行真正的函数。当条件不满足时,它将进入下一行。

考虑这是您的数组:

int a[] = {10,12,9,13,8,7,11,5};

因此,您的方法合并排序将如下所示:

mergeSort(arr a, arr empty, 0 , 7);
mergeSort(arr a, arr empty, 0, 3);
mergeSort(arr a, arr empty,2,3);
mergeSort(arr a, arr empty, 0, 1);

after this `(low + high) / 2 == 0` so it will come out of first calling and going to next:

    mergeSort(arr a, arr empty, 0+1,1);

for this also `(low + high) / 2 == 0` so it will come out of 2nd calling also and call:

    merger(arr a, arr empty,0,0,1);
    merger(arr a, arr empty,0,3,1);
    .
    .
    So on

所以所有排序值都存储在空的 arr 中。它可能有助于理解递归函数的工作原理

于 2018-01-16T06:38:33.137 回答