合并排序中的无限循环?

时间:2015-10-14 02:55:23

标签: java eclipse debugging infinite-loop

我不知道这里发生了什么。此合并排序算法之前已经使用了许多不同类型的输入。我现在用我的"腐败"变量,它不起作用。请帮忙!

我的代码:

overflow:scroll;

我的无限记录(我已经确定了它在哪里循环):

private void mergeWithCorruption(MyNode[] left, MyNode[] right, MyNode[] output) {
    int l = 0;
    int r = 0;
    int o = 0;
    while (l < left.length && r < right.length) {
        if (left[l].getCorruption() == right[r].getCorruption()) {
            // If left == right, add left
            output[o] = left[l];
            l++;
            o++;
        } else if (left[l].getCorruption() < right[r].getCorruption()) {
            // Left is less than right. Add left to output
            output[o] = left[l];
            l++;
            o++;
        } else if (left[l].getCorruption() > right[r].getCorruption()) {
            // Left is greater than right. Add right to output
            output[o] = right[r];
            r++;
            o++;
        }
        System.out.println("l == " + l);
        System.out.println("r == " + r);
        System.out.println(left[0].getCorruption() + " - " + right[0].getCorruption());
    }
    // Finish off the left if there is some remaining
    while (l < left.length) {
        output[o] = left[l];
        o++;
        l++;
    }
    // Finish off the right if there is some remaining
    while (r < right.length) {
        output[o] = right[r];
        o++;
        r++;
    }
}

public void sortByCorruption(MyNode[] arr) {
    if (arr.length > 1) {
        int halfSize = arr.length / 2;
        MyNode[] left = new MyNode[halfSize];
        MyNode[] right = new MyNode[arr.length - halfSize];
        System.arraycopy(arr, 0, left, 0, halfSize);
        System.arraycopy(arr, halfSize, right, 0, arr.length - halfSize);

        sortByCorruption(left);
        sortByCorruption(right);

        mergeWithCorruption(left, right, arr);
    }
}

为什么循环?这两个双打明显相等,所以为什么它不能进入​​上述&#39;如果&#39;声明??

0 个答案:

没有答案