java - 快速排序方法 arrayOutofBounds 异常

标签 java recursion 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)

最佳答案

不应该

 int right = firstIndex + numberToSort -1;

 int right = firstIndex + numberToSort - 1 - pivot;

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

您的另一个选择是将其更改为索引号(我认为这会更直观。)

关于java - 快速排序方法 arrayOutofBounds 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8393804/

相关文章:

Java Swing 和 JComboBox 事件

javascript - 如何减少 Fabric.js 基于递归的函数中的滞后

ruby - 递归数组有什么用?

python - 代码将整数读取为 float

java - 合并步骤后的一些更大的元素不在枢轴的右侧

java - 如何一次获取 500 行,直到获取表中的所有项目?

java - Java 中用户选择最小值和最大值的间隔

java - 如何在java中保存矩阵快速排序的每一步?

java - Firebase Firestore 在运行时错误时崩溃

java - QuickSort Java 实现问题