【问题标题】:How to make merge sort algorithm?如何制作归并排序算法?
【发布时间】:2018-12-13 03:08:45
【问题描述】:

我在下面编写的算法不会向控制台打印任何内容。为什么会这样?如果它不以租赁方式运行所有主要功能。 Sode 问题:合并排序是否正确? 以下是我目前写的:

更新:此代码现在不会出现任何错误,但它只是无休止地运行

public class MergeSort {

    public static int[] mergeSort(int[] list1, int[] list2) {
        int[] list3 = new int[list1.length + list2.length];     
        int num = 0;
        int num2 = 0;
        int x = list1[0];
        int y = list2[0];

        for (int j =0; j< list1.length; j++) {
            while (j != list1.length -1 ||  j != list2.length -1) {             
                if (x<y && num < list1.length) {
                    list3[j] = x;
                    x = list1[num];
                    num += 1;
                } else if (num2 < list2.length) {
                    list3[j] = y;
                    y = list2[num2];
                    num2 += 1;
                }
            }

            if (j == list1.length -1) {
                list3[j] = y;
                y = list2[num2];
            } else if (j == list2.length -1) {
                list3[j] = x;
                num += 1;
                x = list1[num];
            }
        }
        return list3;
    }

    public static void main(String[] args) {
        System.out.println("List 1: 17, 22, 35, 42, 60");
        System.out.println("List 2: 9, 14, 66");
        int[] list1 = {17, 22, 35, 42, 60};
        int[] list2 = {9, 14, 66};
        int[] list3;

        mergeSort(list1, list2);

        list3 = mergeSort(list1, list2);
        System.out.print(" " + list3.length);

        for (int l =0; l< list3.length; l++) {
            System.out.print(" " + list3[l]);
        }
    }

}

【问题讨论】:

  • x = list1[0] 和 y = list2[0]
  • 我不明白这有什么意义。 @安德烈亚斯
  • 等等,我已经这样做了。 @安德烈亚斯
  • 这没有运行,因为它卡在了 while 循环中。按照目前的写法,只有在j = 4和j = 2同时才会中断。
  • @rLevv 我的教授的指示说......“虽然没有找到 L1 或 L2 的末端,但在此期间执行此操作”

标签: java mergesort


【解决方案1】:

你的 for 循环太早了,你的 while 循环没有正确的条件。 由于您的 while 循环正在更新 num 和 num2,因此它的条件应该在这些变量上,因此您要检查 num 或 num2 是否达到相应列表的长度。如果您满足此条件,则意味着您将一个列表完全复制到 list3 中,而另一个列表需要附加到 list3 中。这是您需要 for 循环的地方。下面的代码应该可以完成这项工作。

public static int[] mergeSort(int[] list1, int[] list2) {
    int[] list3 = new int[list1.length + list2.length];
    int num = 0;
    int num2 = 0;
    int j = 0;
    while (num != list1.length && num2 != list2.length) {
        int x = list1[num];
        int y = list2[num2];
        if (x < y) {
            list3[j] = x;
            j += 1;
            num += 1;
        } else {
            list3[j] = y;
            num2 += 1;
            j += 1;
        }
    }

    if (num == list1.length) {
        for (int i = num2; i < list2.length; i++, j++) {
            list3[j] = list2[i];
        }
    } else {
        for (int i = num; i < list1.length; i++, j++) {
            list3[j] = list1[i];
        }
    }
    return list3;
}

【讨论】:

    猜你喜欢
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多