无限循环快速排序

时间:2016-04-22 01:00:11

标签: java data-structures

我在Java编写快速编码,我已经完成了分区部分,但出于某种原因,我的程序一直在运行:

x was swapped with x

其中x是一些随机整数,但每次打印时都是相同的整数。我试图调试它,但它每次在调试器中运行正常。我究竟做错了什么?这是我的代码,不包括父类,数组生成器等。

class QuickSort extends BasicSort {

public QuickSort() {
    super("QuickSort");
}

public void sort() {
    int pivot = data[data.length / 2];
    int low = 0, high = data.length - 1;

    while (true) {
        while (low < (data.length - 1) && data[low] < pivot) {
            System.out.println(data[low] + " is less than " + pivot);
            low++;
        }
        while (high > 0 && data[high] > pivot) {
            System.out.println(data[high] + " is greater than " + pivot);
            high--;
        }

        if (low >= high) {
            break;
        } else {
            int temp = data[low];
            data[low] = data[high];
            data[high] = temp;

            System.out.println(data[low] + " was swapped with " + data[high]);
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

这是一个相同数字的问题,我必须为该条件编写代码。谢谢你们。