algorithm for nth_element [closed]

断了今生、忘了曾经 提交于 2019-12-18 02:02:13

问题


I have recently found out that there exists a method called nth_element in the STL. To quote the description:

Nth_element is similar to partial_sort, in that it partially orders a range of elements: it arranges the range [first, last) such that the element pointed to by the iterator nth is the same as the element that would be in that position if the entire range [first, last) had been sorted. Additionally, none of the elements in the range [nth, last) is less than any of the elements in the range [first, nth).

It claims to have O(n) complexity on average. How does the algorithm work? I could not find any explanation for it.


回答1:


It's called a selection algorithm and wikipedia has a decent page on it: http://en.wikipedia.org/wiki/Selection_algorithm

Also read about order statistics: http://en.wikipedia.org/wiki/Order_statistic




回答2:


Most likely the median-of-medians algo.

http://en.wikipedia.org/wiki/Selection_algorithm#Linear_general_selection_algorithm_-_.22Median_of_Medians_algorithm.22



来源:https://stackoverflow.com/questions/2392485/algorithm-for-nth-element

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