STL 源代码剖析 算法 stl_algo.h -- nth_element
本文为senlie原创。转载请保留此地址:http://blog.csdn.net/zhengsenlie
nth_element
------------------------------------------------------------------------------
描写叙述:又一次排序,使得[nth,last)内没有不论什么一个元素小于[first,nth)内的元素,
但对于[first,nth)和[nth,last)两个子区间内的元素次序则无不论什么保证。
思路:
1.以 median-of-3-partition 将整个序列切割为更小的左、右子序列
2.假设 nth 迭代器落于左序列,就再对左子序列进行切割,否则就再对右子序列进行切割
3.直到切割后的子序列长大于3,对最后这个待切割的子序列做 Insertion Sort
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhlbmdzZW5saWU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
复杂度:O(n)
源代码:
template <class RandomAccessIterator>
inline void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last) {
__nth_element(first, nth, last, value_type(first));
} template <class RandomAccessIterator, class T>
void __nth_element(RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last, T*) {
while (last - first > 3) {
//採用 median-of-3-partition 。參数:(first,last,pivot)
//返回一个迭代器,指向切割后的右段第一个元素
RandomAccessIterator cut = __unguarded_partition
(first, last, T(__median(*first, *(first + (last - first)/2),
*(last - 1))));
if (cut <= nth) //假设 nth 落于右段,再对右段实施切割
first = cut;
else //假设 nth 落于左段。对左段实施切割
last = cut;
}
__insertion_sort(first, last); //对切割后的子序列做 Insertion Sort
} template <class RandomAccessIterator, class T>
RandomAccessIterator __unguarded_partition(RandomAccessIterator first,
RandomAccessIterator last,
T pivot) {
while (true) {
while (*first < pivot) ++first;
--last;
while (pivot < *last) --last;
if (!(first < last)) return first;
iter_swap(first, last);
++first;
}
}
演示样例:
int A[] = {7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5};
const int N = sizeof(A) / sizeof(int);
nth_element(A, A + 6, A + N);
copy(A, A + N, ostream_iterator<int>(cout, " "));
// The printed result is "5 2 6 1 4 3 7 8 9 10 11 12".
STL 源代码剖析 算法 stl_algo.h -- nth_element的更多相关文章
- STL 源代码剖析 算法 stl_algo.h -- search
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie search --------------------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- partial_sort / partial_sort_copy
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partial_sort / partial_sort_copy ------------- ...
- STL 源代码剖析 算法 stl_algo.h -- lower_bound
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie lower_bound(应用于有序区间) ------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- random_shuffle
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie random_shuffle ------------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- merge sort
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- partition
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partition ------------------------------------ ...
- STL 源代码剖析 算法 stl_algo.h -- equal_range
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie equal_range(应用于有序区间) ------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- inplace_merge
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie inplace_merge(应用于有序区间) ----------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- next_permutation
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie next_permutation ----------------------------- ...
随机推荐
- JavaEE-05 分页与文件上传
学习要点 新闻分页显示数据 新闻图片上传 JSP分页显示数据 分页 数据信息较多的的时候一般采用列表显示,方便展示信息: 数据量较大的时候一般采用列表加分页的方式显示,便于阅读. 分页方式:集合或者s ...
- 使用 隧道技术 使用http代理连接 svn:// git://
问题点 在某些情况下 无法通过代理 用 svn 访问svn://协议例如(svn://www.qdac.cc ) 故此有了此贴 远端需要一个代理 服务器 connect-tunnel -P 代理 ...
- java内存模型(线程共享部分)
1.元空间(MetaSpace)与永久代(PermGen)的区别? ----> 1.1 元空间使用的是本机内存(这样的好处是,可以使用的内存空间变大了,没有OutOfMemoryError:Pe ...
- CSS3---渲染属性
1.计数器 CSS3计数器( CSS Counters )可以允许我们使用css对页面中的任意元素进行计数,实现类似于有序列表的功能.与有序列表相比,它的突出特性在于可以对任意元素计数,同时实现个性化 ...
- 使用js获取页面的各种高度
使用js获取相关高度: 获取网页被滚动条卷去的高度——兼容写法: scrollHeight = documen.body.scrollTop || document.documentElement.s ...
- linux下solr5.0.0环境搭建
1解压 linux下解压命令 tar -zxvf solr-5.0.0.tgz 2启动 linux 系统 直接切换solr-5.0.0\bin 文件夹 执行 solr star -p 8983 (如果 ...
- 大数据学习——hdfs集群启动
第一种方式: 1 格式化namecode(是对namecode进行格式化) hdfs namenode -format(或者是hadoop namenode -format) 进入 cd /root/ ...
- JSP中使用<c:forEach>标签循环遍历元素
转载:http://blog.csdn.net/hero_cheng/article/details/51924577
- Dream City(线性DP)
描述 JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the ya ...
- FZU1004-Number Triangle经典动归题,核心思路及代码优化
Problem 1004 Number Triangle Accept: 2230 Submit: 5895Time Limit: 1000 mSec Memory Limit : 327 ...