我知道當IntroSort被類似T []的東西調用時,比如Integer [] x;它會對數組進行排序,直到遞歸深度過多(在大多數實現中變爲0),然後它將切換到HeapSort。但是,當遞歸深度變爲2log2n時,我試圖調用修改後的MergeSort。修改後的MergeSort只使用原始數組大小一半的臨時數組,只是爲了節省一點時間和空間。無論如何,我基本上覆制了所有的QuickSort,除了在遞歸調用之前添加了depth_limit檢查。從IntroSort到MergeSort
private void quicksort(T[] items, int left, int right) {
int depth_limit = (int) (2*Math.log(items.length));
if(depth_limit == 0)
{
mergesorter.sort(items, left, right);
return;
}
int pivotindex = findpivot(items, left, right);
// curr will be the final position of the pivot item.
int curr = partition(items, left, right, pivotindex);
if ((curr - left) > 1) {
quicksort(items, left, curr - 1); // Sort left partition
}
if ((right - curr) > 1) {
quicksort(items, curr + 1, right); // Sort right partition
}
}
我認爲這會工作,因爲我相信
depth_limit = 2 *的log 2(n),其中n是輸入
所以我的問題是,我在哪裏檢查數量遞歸深度切換到MergeSort和我正確計算我的深度?