找到你要的答案

Q:My Merge Sort code is not working (Java)

Q:我的归并排序代码不工作(java)

I have just written up this code an have been trying for ages to try and find out why it is not printing the sorted list. It is running and there are no bugs, except it just does not print out the sorted list. Can you please help me find whats wrong?

public class Merging {
public static void main(String[] args) {
    int[] a = new int[10];

       populate(a);
        printA(a);



    a = merge_sort(a);
    printA(a);

}

public static int[] merge_sort(int[] B) {
    if (B.length <= 1) {
        return B;

    }

    int midpoint = B.length / 2;
    int[] left = new int[midpoint];
    int[] right= new int[B.length-midpoint];


    int[] result;


    for (int i = 0; i < midpoint; i++) {
        left[i] = B[i];

    }
    int x = 0;
    for (int j = midpoint; j < B.length; j++) {

        if (x < right.length) {


            right[x] = B[j];
            x++;
        }
    }

    left = merge_sort(left);
    right = merge_sort(right);

    result = merge(left, right);

    return result;

}

public static int[] merge(int[] left, int[] right) {

    int lengthResult = left.length + right.length;
    int[] result   = new int[lengthResult];
    int indexL = 0;
    int indexR = 0;
    int indexRes = 0;


    while (indexL < left.length || indexR < right.length) {

        if (indexL < left.length && indexR < right.length) {
            if (left[indexL] <= right[indexR]) {

                result[indexRes] = left[indexL];
                indexL++;
                indexRes++;


            } else {
                result[indexRes] = right[indexR];
                indexR++;
                indexRes++;

            }


        } else if (indexL < left.length) {

            result[indexRes] = left[indexL];
            indexL++;
            indexRes++;

        }


    }

    return result;

}

public static void printA(int[] B) {

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

public static int[] populate(int[] B) {

    for (int i = 0; i < B.length; i++) {
        B[i] = (int) (Math.random() * 100);
    }
    return B;
}
}

我刚刚编写了这个代码,我已经试了很久了,试图找出为什么它没有打印排序列表。它运行和没有错误,只是它不打印出排序列表。你能帮我找出什么不对吗?

public class Merging {
public static void main(String[] args) {
    int[] a = new int[10];

       populate(a);
        printA(a);



    a = merge_sort(a);
    printA(a);

}

public static int[] merge_sort(int[] B) {
    if (B.length <= 1) {
        return B;

    }

    int midpoint = B.length / 2;
    int[] left = new int[midpoint];
    int[] right= new int[B.length-midpoint];


    int[] result;


    for (int i = 0; i < midpoint; i++) {
        left[i] = B[i];

    }
    int x = 0;
    for (int j = midpoint; j < B.length; j++) {

        if (x < right.length) {


            right[x] = B[j];
            x++;
        }
    }

    left = merge_sort(left);
    right = merge_sort(right);

    result = merge(left, right);

    return result;

}

public static int[] merge(int[] left, int[] right) {

    int lengthResult = left.length + right.length;
    int[] result   = new int[lengthResult];
    int indexL = 0;
    int indexR = 0;
    int indexRes = 0;


    while (indexL < left.length || indexR < right.length) {

        if (indexL < left.length && indexR < right.length) {
            if (left[indexL] <= right[indexR]) {

                result[indexRes] = left[indexL];
                indexL++;
                indexRes++;


            } else {
                result[indexRes] = right[indexR];
                indexR++;
                indexRes++;

            }


        } else if (indexL < left.length) {

            result[indexRes] = left[indexL];
            indexL++;
            indexRes++;

        }


    }

    return result;

}

public static void printA(int[] B) {

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

public static int[] populate(int[] B) {

    for (int i = 0; i < B.length; i++) {
        B[i] = (int) (Math.random() * 100);
    }
    return B;
}
}
answer1: 回答1:

imagine the case in your loop, where indexL < left.length == false but indexR < right.length == true, you never increase indexR and the loop will never terminate

想象在你的循环的情况下,在索引& lt;left.length = =假但indexr <;right.length = =真的,你永远不会增加indexr和循环将永远不会结束

java  algorithm  mergesort