Java合并排序递归奇怪

时间:2018-06-21 05:14:11

标签: java recursion mergesort

我正在对排序算法进行一些评论,因为它们实际上在我的生活中并不经常出现,这一点使我发疯。我也有一段时间没有做Java了,所以我想也许我忘记了某些语言,但是我不这么认为。

我发现成功或失败取决于我如何进行递归调用以拆分数组。因此,如果我将split调用的返回值用作合并调用的参数,那么它将起作用。但是,如果我先递归调用split,然后再调用merge,它将失败。但是,在我看来,它们都应该起作用。这似乎与堆栈的展开方式有关,但是我无法完全绕开它。

static Comparable[] mergesort(Comparable[] src) {
    if (src.length < 2)
        return src;

    int middle = src.length / 2;
    if (src.length % 2 > 0)
        middle++;

    Comparable[] left = new Comparable[src.length / 2];
    Comparable[] right = new Comparable[middle];

    System.arraycopy(src, 0, left, 0, left.length);
    System.arraycopy(src, src.length / 2, right, 0, right.length);

    // THIS DOESN'T WORK, BUT I DON'T KNOW WHY
    // mergesort(left);
    // mergesort(right);
    // return mergearrays(left, right);
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    // 
    // THIS ONE DOES WORK
    // 
    return mergearrays(mergesort(left), mergesort(right));
}

static Comparable[] mergearrays(Comparable[] left, Comparable[] right) {

    Comparable[] retval = new Comparable[left.length + right.length];

    int i = 0, j = 0, k = 0;
    for (; i < left.length && j < right.length;) {
        if (left[i].compareTo(right[j]) >= 0) {
            retval[k++] = right[j++];
        } else
            retval[k++] = left[i++];

    }

    while (k < retval.length) {
        if (i < left.length) {
            retval[k++] = left[i++];
        }
        if (j < right.length) {
            retval[k++] = right[j++];
        }
    }

    return retval;
}

1 个答案:

答案 0 :(得分:1)

您的mergearrays方法创建一个新数组,在其中存储合并的排序数组。因此,如果您不使用mergesort(left)mergesort(right)返回的数组(它们本身是由先前对mergearrays的调用创建的数组),则将丢弃已排序的数组并传递未排序的数组( leftright)到mergearrays

因此此代码是错误的:

mergesort(left); // returns a new sorted array which you ignore, doesn't modify left
mergesort(right); // returns a new sorted array which you ignore, doesn't modify right
return mergearrays(left, right); // merges two unsorted arrays, and therefore is wrong

此代码正确无误:

// merges the two sorted arrays returned by mergesort(left) and mergesort(right)
return mergearrays(mergesort(left), mergesort(right));

一些合并排序实现对原始数组执行合并步骤(即,它们对数组进行排序而不是返回新的排序数组),在这种情况下,mergearrays方法和mergesort都不需要返回任何东西,以下代码可以工作:

mergesort(left);
mergesort(right);
mergearrays(left, right);