所以当我只有超过10,000个排序时,我得到一个堆栈溢出。
我正在用随机支点做一本书的分区。
public static void QuickSortbookRand(int[]a, int low, int high) //random pointer, book's partition
{
int le = low, diff = high-low, i=0;
Random rn = new Random();
if (diff > 0)
{
int pivloc = rn.nextInt(diff + 1) + low;
switchnum(a, pivloc, high);
pivloc = high;
int piv = a[pivloc];
for (i = low; i<high; i++)
{
if (a[i] < piv)
{
switchnum(a, i, le);
le++;
}
}
switchnum(a,le,pivloc);
pivloc=le;
/*
if (diff < 2 && high > 10700)
{
System.out.println("high:" + high + ",low:" + low + ",pivloc:" + pivloc);
}
*/
if(low < pivloc)
{
QuickSortbookRand(a, low, pivloc-1);
}
if(high > pivloc+1)
{
QuickSortbookRand(a, pivloc+1, high);
}
}
return;
}
当我取消测试打印语句时,我得到一个错误:
high:10708,low:10707,pivloc:10707
high:10711,low:10710,pivloc:10710
high:10714,low:10713,pivloc:10713
high:10719,low:10718,pivloc:10718
Exception in thread "main" java.lang.StackOverflowError
at ArraySorts.QuickSortbookRand(ArraySorts.java:155)
at ArraySorts.QuickSortbookRand(ArraySorts.java:155)
at ArraySorts.QuickSortbookRand(ArraySorts.java:155)
at ArraySorts.QuickSortbookRand(ArraySorts.java:155)
我需要应用程序在10,000,000个元素阵列上运行。有没有办法减少递归调用?
对10个元素下的所有数组使用insertionsort。
high:10188,low:9689,pivloc:9689
high:10188,low:9690,pivloc:9690
high:10188,low:9691,pivloc:9691
high:10188,low:9692,pivloc:9692
high:10188,low:9693,pivloc:9693
high:10188,low:9694,pivloc:9694
Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.ext.DoubleByte$Encoder.encodeLoop(DoubleByte.java:514)
at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:561)
at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
at java.io.PrintStream.write(PrintStream.java:526)
at java.io.PrintStream.print(PrintStream.java:669)
at java.io.PrintStream.println(PrintStream.java:806)
at ArraySorts.QuickSortbookRand(ArraySorts.java:161)