算法nth_element [英] algorithm for nth_element

查看:267
本文介绍了算法nth_element的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现,存在一个名为 nth_element 在STL方法。引述说明:

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

Nth_element类似于   partial_sort,因为它部分地   承揽了一系列的元素:它   排列范围[第一个,最后一个),例如   该元素由指向   迭代的第n个相同的   元素,这将是在那个位置   如果整个区间[first,last)中有   被排序。此外,没有任何的   范围内的元素[n次,最后一次)是   小于任何在元素   范围[第一次,第n次)。

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).

这号称有平均为O(n)的复杂性。如何算法的工作?我找不到它的任何解释。

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

推荐答案

这就是所谓的一个选择算法和维基百科有一个体面的页面就可以了:<一href="http://en.wikipedia.org/wiki/Selection_algorithm">http://en.wikipedia.org/wiki/Selection_algorithm

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

另外阅读顺序统计:<一href="http://en.wikipedia.org/wiki/Order_statistic">http://en.wikipedia.org/wiki/Order_statistic

这篇关于算法nth_element的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆