快速排序方法arrayOutofBounds Exception

时间:2011-12-06 00:22:12

标签: java recursion quicksort

我正在尝试使用quicksort方法对某些或部分数据进行排序。我认为我的分区方法是正确的,我的快速调用快速排序左段和右段给我一个超出界限的数组异常,但我似乎无法搞清楚。这是我写的代码,我在

中得到了错误
 private void quickSorter(String[] data, int firstIndex, int numberToSort) {
    if (numberToSort > 1) {
        int pivot = partition(data, firstIndex, numberToSort);
        int right = firstIndex + numberToSort -1;
        if (firstIndex < pivot -1)
        quickSorter(data, firstIndex, pivot -1);
        if (pivot  < right && right <data.length)
        quickSorter(data, pivot  , right);
    }
}
private void swap(String[] data ,int tooBigNdx, int tooSmallNdx) {
    String temp = data[tooBigNdx];
    data[tooBigNdx] = data[tooSmallNdx];
    data[tooSmallNdx] = temp;

}

@Override
public int partition(String[] data, int firstIndex, int numberToPartition) {
    int ndx = firstIndex;
    String pivot = data[ndx];
    int tooBigNdx = ndx;
    int tooSmallNdx = firstIndex + numberToPartition -1;
    while (tooBigNdx < tooSmallNdx) {
    while (tooBigNdx < tooSmallNdx && (data[tooBigNdx].compareTo(pivot) < 0 || data[tooBigNdx].equals(pivot)))
        tooBigNdx++;

    while (tooSmallNdx > ndx && (data[tooSmallNdx].compareTo(pivot) > 0 || data[tooSmallNdx].equals(pivot))) 
        tooSmallNdx--;
    if (tooBigNdx < tooSmallNdx ) {
        swap(data, tooBigNdx,tooSmallNdx);
    tooBigNdx++;
    tooSmallNdx--;
    }
    }
    if (pivot.compareTo(data[tooSmallNdx]) > 0 || pivot.equals(data[tooSmallNdx]) ) {
    swap(data, ndx,tooSmallNdx);
    return tooSmallNdx;
    } else return ndx;


}

编辑:

每次随机创建用于测试的数据集,并且是5个字母的单词。我总是得到一个arrayoutofBounds异常,但它超出界限的数量会根据数据集而变化。

java.lang.ArrayIndexOutOfBoundsException: -1
    at sortcomparison.BasicSorter.partition(BasicSorter.java:62)
    at sortcomparison.BasicSorter.quickSorter(BasicSorter.java:37)
    at sortcomparison.BasicSorter.quickSorter(BasicSorter.java:40)
    at sortcomparison.BasicSorter.quickSorter(BasicSorter.java:40)
    at sortcomparison.BasicSorter.quickSorter(BasicSorter.java:40)
    at sortcomparison.BasicSorter.quickSort(BasicSorter.java:31)
    at sbccunittest.BasicSorterTester.standardSortTest(BasicSorterTester.java:166)
    at sbccunittest.BasicSorterTester.testQuickSort(BasicSorterTester.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2 个答案:

答案 0 :(得分:1)

不应该

 int right = firstIndex + numberToSort -1;

 int right = firstIndex + numberToSort - 1 - pivot;

请记住,第二个参数不是索引号,而是一个计数。

您可以选择将其更改为索引编号(我认为这更直观。)

答案 1 :(得分:1)

我认为您的问题出现在&& , ||

之间的while循环中的Operator Precedence中

用括号解决。

对于第二个问题,我认为在第三个问题时替换:

while (tooSmallNdx > ndx && (data[tooSmallNdx].compareTo(pivot) > 0 || data[tooSmallNdx].equals(pivot))) 

使用:

while (tooSmallNdx > ndx && data[tooSmallNdx].compareTo(pivot) > 0)