1
你好我正在尝试在Java中实现Quicksort方法,但由于某种原因,当我只做5个元素时,出现堆栈溢出错误!任何想法可能是错的?我的代码的 部分:当我实现Quicksort方法时堆栈溢出
class QuickSort implements AbstractSort {
public int partition(int[] numbers, int start, int end)
{
int pivot = numbers[end];
int theIndex = start;
int temp;
for(int i = start; i <= end - 1; i++)
{
if(numbers[i] <= pivot)
{
temp = numbers[theIndex];
numbers[theIndex] = numbers[i];
numbers[i] = temp;
theIndex++;
}
}
temp = numbers[theIndex];
numbers[theIndex] = pivot;
numbers[end] = temp;
return theIndex;
}
public void mySort(int[] numbers, int start, int end)
{
if(start < end)
{
int myIndex = partition(numbers, start, end);
mySort(numbers, 0, myIndex-1);
mySort(numbers, myIndex + 1, numbers.length - 1);
}
else
{
return;
}
}
public void sort(int[] numbers)
{
mySort(numbers, 0, numbers.length - 1);
}
}
你可以提供一些例子,你正在试图理清的问题? –
我用过9 0 8 5 6 ...但我已经找到了问题!我不应该在递归快速排序中传递0,而是“开始”。所以而不是mySort(数字,0,myIndex-1);它应该是mySort(数字,开始,myIndex-1); – AVC