Pio*_*otr 2 java stack-overflow arrays quicksort
有没有人知道为什么我会在以下代码中的快速排序上出现堆栈溢出?:
private int[] concat( int[] less, int inxl, int pivot, int inxm, int[] more )
{
int[] concated = new int[ less.length ];
for( int inx = 0; inx < inxl; inx++ )
{
concated[ inx ] = less[ inx ];
}
concated[ inxl ] = pivot;
inxl++;
for( int inx = 0; inx < inxm; inx++ )
{
concated[ inxl ] = more[ inx ];
inxl++;
}
return concated;
}
private int[] quickSort( int[] array )
{
if( array.length <= 1 )
return array;
int[] less = new int[ array.length ];
int[] more = new int[ array.length ];
int inxl = 0, inxm = 0;
for( int inx = 1; inx < array.length; inx++ )
{
if( array[ inx ] < array[ 0 ] )
{
less[ inxl ] = array[ inx ];
inxl++;
}
else
{
more[ inxm ] = array[ inx ];
inxm++;
}
}
return concat( quickSort( less ), inxl, array[ 0 ], inxm, quickSort( more ) );
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.我正在修改面试并且有点生疏,所以时间非常重要.先谢谢!:)
真诚的,彼得.
你的quickSort方法的递归是错误的.它应该使用较小的数组(或者使用其他一些较小的参数)调用自身,而不是使用相同长度的数组.这就是为什么你得到一个无休止的递归,它显示为一个StackOverflowError.