简体   繁体   English

如何制作合并排序算法?

[英]How to make merge sort algorithm?

The algorithm that I have written bellow does not print anything to the console. 我在下面写的算法不会向控制台打印任何内容。 Why is that the case? 为什么会这样? should it not at leased run all that is in main. 如果它不是在租赁运行所有主要的。 Sode question: is teh merge sort correct? 索德问题:合并排序是否正确? Below is what I have written so far: 以下是我到目前为止所写的内容:

Update: This code does noyt now get any errors however it just runs endelessly 更新:此代码现在没有任何错误,但它只是无助地运行

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]);
        }
    }

}

Your for loop is too early and your while loop does not have the correct condition. 你的for循环太早了,你的while循环没有正确的条件。 Since your while loop is updating num and num2, its condition should be on those variables so you want to check if either num or num2 reached the length of the corresponding list. 由于while循环正在更新num和num2,因此它的条件应该是那些变量,因此您要检查num或num2是否达到相应列表的长度。 If you met this condition then it means you copied entirely one list into list3 and the other list need to be appended to list3. 如果您遇到这种情况,则表示您将一个列表完全复制到list3中,另一个列表需要附加到list3。 Here is where you need your for loop. 这是您需要for循环的地方。 The code below should do the job. 下面的代码应该完成这项工作。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM