简体   繁体   中英

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. 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. 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. Here is where you need your for loop. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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