When does introsort shift from quicksort to heapsort?

99封情书 提交于 2019-12-19 18:58:08

问题


Introsort begins with quicksort and switches to heapsort when the recursion depth exceeds a level based on the number of elements being sorted. What is that number? Is there a specific range or a limiting value?


回答1:


The point at which the Introsort algorithm switches from Quicksort to Heapsort is determined by depth_limit:

depth_limit = 2 · ⎣log2(l)⎦

Where l is the length of the sequence that is to be sorted, so l‍=‍n for the whole sequence. With each recursive call depth_limit is decremented by one. When depth_limit reaches 0, it switches from Quicksort to Heapsort.




回答2:


I just tried reading the introductory Wikipedia article. It says

It counts the recursion depth. If a logarithmic depth is exceeded, the algorithm switches to Heapsort from Quicksort to keep the worst case outcome of Quicksort down

and through the Musser's original paper on Introsort.

It says that introsort is slower than heapsort because it performs 2*log(2,N) computations before it switches to heapsort.

My understanding is that recursion depth is 2*log(2,N)

For N=300 elements to sort, it will be 2*8 = 16



来源:https://stackoverflow.com/questions/11175478/when-does-introsort-shift-from-quicksort-to-heapsort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!