|
|
|
|
|
by lorenzhs
3391 days ago
|
|
No, as per my previous comment, it t does not use a heap. It's like quicksort, but only doing one of the recursive calls: - choose a pivot p randomly from the elements - partition into elements <= p and > p. This takes time O(n). - if the number of elements <= p is at most k (the rank of the element we want), do a recursive call on these elements. Otherwise recurse on the other elements (those >p). Because a random pivot splits the elements well enough most of the time, this has an expected running time of O(n): 1 + ½ + ¼ + ⅛ + … < 2 in the best case, and similar constants if the partitioning is a little worse. But in the worst case, when the pivot is the smallest/largest element in each iteration, it's O(n²). That's where the fanciness of introselect comes in: if quickselect doesn't converge, it switches to an algorithm with worst-case O(n) time, which is slower in most cases but saves the day when quickselect has trouble. |
|
It's hairier than a partial quicksort, but (if you erase the proof and weigh the chalk dust) guaranteed linear time.