归并排序中的递归

发布于 2025-01-12 09:10:50 字数 1688 浏览 1 评论 0原文

public static void main(String[] args) {
    int[] numbers = {20,4,7,6,1,3,9,5};
    mergeSort(numbers);
}
private static void mergeSort(int[] inputArray) {
    int inputLength = inputArray.length;
    System.out.println(inputLength);

    if (inputLength < 2)
        return;


    int midIndex = inputLength / 2;

    int[] leftHalf = new int[midIndex];
    int[] rightHalf = new int[inputLength - midIndex];

    for (int i = 0; i < midIndex; i++) {
        leftHalf[i] = inputArray[i];

    }
    for (int i = midIndex; i < inputLength; i++) {
        rightHalf[i - midIndex] = inputArray[i];

    }

    mergeSort(leftHalf);
    mergeSort(rightHalf);
    merge(inputArray, leftHalf, rightHalf);

}
private static void merge(int[] inputArray, int[] leftHalf, int[] rightHalf) {
    int leftSize = leftHalf.length;
    int rightSize = rightHalf.length;

    int i = 0, j = 0, k = 0;

    while (i < leftSize && j < rightSize) {
        if (leftHalf[i] <= rightHalf[i]) {
            inputArray[k] = leftHalf[i];
            i++;
        } else {
            inputArray[k] = rightHalf[j];
            j++;
        }
        k++;
    }
    while (i < leftSize) { //eğer karşılaştırılmayan bir tane kalırsa diye yapılıyor yani 
        inputArray[k] = leftHalf[i];
        i++;
        k++;
    }
    while (j < rightSize) {
        inputArray[k] = rightHalf[j];
        j++;
        k++;
    }

}

mergeSort 部分中,如果 inputLength 2部分代码,当长度小于2时我们返回。而上次inputLength是1,它变成2并返回数组[20,4 ]

从逻辑上讲,这对我来说没有意义。当最后剩下 [20] 时,它如何回到 [20,4]

public static void main(String[] args) {
    int[] numbers = {20,4,7,6,1,3,9,5};
    mergeSort(numbers);
}
private static void mergeSort(int[] inputArray) {
    int inputLength = inputArray.length;
    System.out.println(inputLength);

    if (inputLength < 2)
        return;


    int midIndex = inputLength / 2;

    int[] leftHalf = new int[midIndex];
    int[] rightHalf = new int[inputLength - midIndex];

    for (int i = 0; i < midIndex; i++) {
        leftHalf[i] = inputArray[i];

    }
    for (int i = midIndex; i < inputLength; i++) {
        rightHalf[i - midIndex] = inputArray[i];

    }

    mergeSort(leftHalf);
    mergeSort(rightHalf);
    merge(inputArray, leftHalf, rightHalf);

}
private static void merge(int[] inputArray, int[] leftHalf, int[] rightHalf) {
    int leftSize = leftHalf.length;
    int rightSize = rightHalf.length;

    int i = 0, j = 0, k = 0;

    while (i < leftSize && j < rightSize) {
        if (leftHalf[i] <= rightHalf[i]) {
            inputArray[k] = leftHalf[i];
            i++;
        } else {
            inputArray[k] = rightHalf[j];
            j++;
        }
        k++;
    }
    while (i < leftSize) { //eğer karşılaştırılmayan bir tane kalırsa diye yapılıyor yani 
        inputArray[k] = leftHalf[i];
        i++;
        k++;
    }
    while (j < rightSize) {
        inputArray[k] = rightHalf[j];
        j++;
        k++;
    }

}

In the mergeSort part if inputLength < 2 part of the code, we return when the length is less than 2. And last time the inputLength was 1, it becomes 2 and returns to the array [20,4].

This did not make sense to me logically. How does it get back to [20,4] when last we had [20] left?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花之痕靓丽 2025-01-19 09:10:50

首先你分享的代码在合并功能部分有缺陷,你可以在网上找到适合合并排序的代码,你可以参考
https://www.geeksforgeeks.org/java-program-for-merge -sort/

现在为了理解合并排序,您必须了解堆栈(后进先出)和堆栈的概念。递归。在递归中,递归调用之后的行会等待,直到对函数的递归调用尚未完全执行。因此,在第一次调用合并排序的情况下,

数组的长度为 n,并等待大小为 (n/2) 的 mergeSort(leftHalf) 和 mergeSort(rightHalf) 的完整执行。

现在对于 mergeSort(leftHalf) 和 mergeSort(rightHalf)
将会有子左部分和子右部分,这将持续到数组的大小变得<2并且剩余部分将等待。

成功执行最小部分后,它将返回到调用该部分的上一个部分。最终这将返回到第一次调用该函数的地方。

在您的代码中,两个较小的数组都会合并到较大的数组中,因此左右子数组的数据不会丢失。

first of all the code you have shared is flawed in the merge function part, you can find the proper code for Merge sort online, you can refer to
https://www.geeksforgeeks.org/java-program-for-merge-sort/

Now for understanding merge sort you have to understand the concept of stack (Last in First out) & recursion. In recursion the lines after the recursive call wait till the recursive call to the function has not executed completely. So in case of the

1st call Merge sort the length of the array is n and waiting for the complete execution of mergeSort(leftHalf) and mergeSort(rightHalf) both of size (n/2).

now for both the mergeSort(leftHalf) and mergeSort(rightHalf)
there will be sub left part and sub right part and this will continue till the size of the array becomes <2 and the remaining part will wait.

and after the successful execution of the smallest part it will return to the previous part from where this part was called. By this eventually this will return to the place where the function was called first.

And in case of your code both the smaller arrays are merged into the larger array so the data of the left and right sub array aren't lost.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文