开发者

STL Sort vs median of medians

开发者 https://www.devze.com 2023-01-16 11:29 出处:网络
开发者_如何学Pythonstd::sort() uses the Introsort algorithm that switches between quick & heap sort depending upon the current partitioning ratio.
开发者_如何学Python

std::sort() uses the Introsort algorithm that switches between quick & heap sort depending upon the current partitioning ratio.

Is there a practical disadvantage to implementing Median-of-Medians Quicksort in place of Introsort? After all, it's harder to model a mixture of sorting algorithms theoretically & compute their worst case complexity - though I assume Introsort's would be O(N log N).


See Musser's original paper on Introsort. Basically, though Median of Medians and Introsort are the same asymptotically, Introsort does less constant work per item and is generally faster. The paper does mention that Median of Medians would be fine as a fallback sort instead of Heap Sort.

David R. Musser. Introspective Sorting and Selection Algorithms. [Postscript version]


From what I remember, Quicksort performs badly on collections that are sorted or very nearly sorted - O(n^2). Check out Wikipedia's page on Introsort - with a compelling statistic on Introsort vs. Quicksort.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号