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