Quicksort实现程序数组超出范围错误并且不对生成的数字进行排序

时间:2018-06-29 00:51:32

标签: java sorting quicksort

我是编程新手,在尝试编写Quicksort程序时似乎无法弄清我的错误。我完成了大部分的实现,除了一些我似乎无法弄清楚的错误。这是我当前的代码:

public class Quicksort {
    public static void main(String[] args) {

        Integer[] numbers = new Integer[20];
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = (int) (Math.random() * 100);
        }


        printArray(numbers);


        quicksort(numbers);


        printArray(numbers);
    }


    public static <T> void printArray(T[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + (i < array.length - 1 ? ", " : "\n"));
        }
    }


    public static <T> void printArrayPartition(T[] array, int low, int high, int pivot) {
        for (int i = 0; i < array.length; i++) {
            String element = "" + array[i];
            if (i == pivot)
                element = "*" + element + "*";
            if (i == low)
                element = "[" + element;
            if (i == high)
                element += "]";
            System.out.print(element + (i < array.length - 1 ? ", " : "\n"));
        }
    }

    public static <T extends Comparable<T>> void quicksort(T[] array) {
        quicksort(array, 0, array.length - 1);
    }

    private static <T extends Comparable<T>> void quicksort(T[] array, int low, int high) {

        if (low < high) {
            int pivotIndex = partition(array, low, high);
            int pivot = pivotIndex;

            printArrayPartition(array, low, high, pivot);

            quicksort(array, low - 1, pivotIndex - 1);
            quicksort(array, pivotIndex + 1, high + 1);
        }
    }

    private static <T extends Comparable<T>> int partition(T[] array, int low, int high) {

        T pivotValue = array[high];

        int left = low;


        for (int i = 0; i < array.length; i++) {

            if (array[i].compareTo(pivotValue) > 0) {

                left++;
            }
        }

        swap(array, high, left);

        return left;
    }

    private static <T> void swap(T[] array, int a, int b) {
        T temp = array[a];
        array[a] = array[b];
        array[b] = temp;
    }
}

运行该程序时,出现ArrayIndexOutOfBounds错误,我不确定为什么。我已经追踪了几次我的代码,但仍然无法弄清楚。

堆栈跟踪:

54, 91, 26, 43, 98, 65, 88, 78, 88, 60, 58, 75, 74, 46, 79, 81, 64, 49, 67, 31
[54, 91, 26, 43, 98, 65, 88, 78, 88, 60, 58, 75, 74, 46, 79, 81, 64, 49, *31*, 67]
54, 91, 26, 43, 98, 65, 88, 78, 88, 60, 58, 75, 74, 46, *49*, 81, 64, 79], 31, 67
54, 91, 26, 43, 98, 65, 88, 78, 88, 60, 58, 75, 74, 49], *46*, 81, 64, 79, 31, 67
54, 91, 26, 43, 98, 65, 88, 78, 88, 60, 58, 75, *49*, 74], 46, 81, 64, 79, 31, 67
54, 91, 26, *75*, 98, 65, 88, 78, 88, 60, 58, 43], 49, 74, 46, 81, 64, 79, 31, 67
54, 91, 46], 75, 98, 65, 88, 78, 88, 60, 58, 43, 49, 74, *26*, 81, 64, 79, 31, 67
54, 91, *74*, 75, 98, 65, 88, 78, 88, 60, 58, 43, 49, 46], 26, 81, 64, 79, 31, 67
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -6
    at Quicksort.swap(Quicksort.java:79)
    at Quicksort.partition(Quicksort.java:72)
    at Quicksort.quicksort(Quicksort.java:47)
    at Quicksort.quicksort(Quicksort.java:52)
    at Quicksort.quicksort(Quicksort.java:52)
    at Quicksort.quicksort(Quicksort.java:52)
    at Quicksort.quicksort(Quicksort.java:52)
    at Quicksort.quicksort(Quicksort.java:52)
    at Quicksort.quicksort(Quicksort.java:52)
    at Quicksort.quicksort(Quicksort.java:52)
    at Quicksort.quicksort(Quicksort.java:41)
    at Quicksort.main(Quicksort.java:13)

1 个答案:

答案 0 :(得分:2)

在您的递归调用中:

quicksort(array, low - 1, pivotIndex - 1);
quicksort(array, pivotIndex + 1, high + 1);

您从lowpivotIndex中减去一个,然后将这些值传递给

swap(array, high, left);  //via int left = low;

依次调用highlow的索引。启动方法时,low设置为零。然后您减去一个得到负数。如果从未满足以下条件:if (array[i].compareTo(pivotValue) > 0),那么将不会增加left,并且您将向swap(…)传递一个负数。在swap()方法中:

T temp = array[a];   //a is left here
    array[a] = array[b];
    array[b] = temp;

您直接尝试调用可能为负数的left索引,因此调用具有负索引的indexoutofboundsexception

quicksort(array, pivotIndex + 1, high + 1);可以以相同的方式向high添加一个,从而使其超出范围