有人知道为什么在下面的代码中我的快速排序会出现堆栈溢出吗?
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 ) );
}任何帮助都将不胜感激。我正在复习面试,有点生疏了,所以时间是非常重要的。首先感谢!:)
真心的,皮奥特。
发布于 2011-05-22 02:43:56
您的quickSort方法的递归错误。它应该使用更小的数组(或使用其他变小的参数)来调用自己,而不是使用相同长度的数组。这就是为什么你会得到一个无休止的递归,它显示为一个StackOverflowError。
https://stackoverflow.com/questions/6083686
复制相似问题