有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java快速排序方法arrayOutofBounds异常

我正在尝试使用快速排序方法对部分或部分数据进行排序。我认为我的分区方法是正确的,我对左段和右段进行快速排序的递归调用给了我一个数组越界异常,但我似乎无法理解它。这是我写的代码,我正在得到错误

 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) 个答案

  1. # 1 楼答案

    我认为您的问题在于&& , ||之间的while循环中的运算符优先级

    用括号解决

    对于第二个问题,我在第三个问题中考虑替换:

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

    与:

    while (tooSmallNdx > ndx && data[tooSmallNdx].compareTo(pivot) > 0)
    
  2. # 2 楼答案

    不应该

     int right = firstIndex + numberToSort -1;
    

     int right = firstIndex + numberToSort - 1 - pivot;
    

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

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