2011-12-06 2 views
0

一部または一部のデータを並べ替えるクイックソートメソッドを取得しようとしています。私は私のパーティションメソッドが正しいと思うと私の再帰呼び出しquicksortの左のセグメントと右のセグメントは私に境界を外に配列を与えている例外しかし、私はそれを把握するように見えることはできません。ここで私が書いたコードがあるとクイックソートメソッド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; 


} 

EDIT内のエラーを取得しています:テストのための

データセットは、ランダムに毎回作成し、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) 
+1

スタックトレースを投稿してください、そして上記の例外がスローされたコードのどの行を示している:

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

で:私は第三しばらく考える第二の問題については

を交換してください。テストしているデータも提供してください。 –

+0

私は編集を投稿しました。 – Pengume

答えて

1

int right = firstIndex + numberToSort -1; 

int right = firstIndex + numberToSort - 1 - pivot; 

は、第二のパラメータを覚えてされるべきではない。それは、カウントされ、インデックス番号ではありません。

あなたは私はあなたの問題はで解決&& , ||

間whileループで演算子の優先順位であると思い、他のオプションのインデックス番号にそれを変更することです(私はより直感的なことだと思う。)

+0

これはOut of boundsエラーを取り除きましたが、どういうわけか私の言葉はソートされません。助けてくれてありがとう! – Pengume

+0

は、初期のアレイの範囲外の問題を解決したため、答えとしてマークされています。ありがとう。 – Pengume

1

です括弧。

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

ちょっとRedHat、上記のHoganは実際には私の実際に解決されていませんが、現在私のarrayoutofboundsエラーを解決しました。私はかっこを入れました上記のコードをあなたが話していると思われるものを反映するように変更しました。 – Pengume

+0

私の返事を更新しました、あなたの問題を解決することを願っています。 –

+0

は修正されませんでしたが、とにかく感謝します。愚かなクイックソート! – Pengume

関連する問題