Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法
*/-->
div.org-src-container {
font-size: 85%;
font-family: monospace;
}
pre.src {
background-color:#f8f4d7
}
p {font-size: 15px}
li {font-size: 15px}
Table of Contents
1 partial_sort
partial sort 可以进行部分排序,例如,仅 按顺序排出 某个容器中的前 20 名:
bool qualityCompare(const Widget& lhs, const Widget& rhs); partial_sort(widgets.begin(), widgets.begin()+20, widgets.end(), qualityCompare);
2 nth_element
与 partial_sort 类似, nth_element 也用于部分排序,但它只保证指定的排名前 N 个元素会存放到容器的前N,但这前 N 个元素之间的顺序不一定。更重要的是,它可以保证,如果我们对容器进行完全排序,排在 N 位置的那个元素一定和 nth_element 排序后
N 位置的元素相同(等值):
nth_element(widgets.begin(),
widgets.begin()+20,
widgets.end(),
qualityCompare);
此外, nth_element 可以方便地计算出指定容器中指定排名的元素:
vector<Widget>::size_type goalOffset = 0.21 * widgets.size();
nth_element(begin, begin+goalOffset, end, qualityCompare);
3 stability
对于任意的等价对象 A 与 B ,如果 A 在容器中的位置比 B 靠前,且排序后 A 的位置仍然在 B 之前,则称该排序算法为稳定的 (stable) 。 partial_sort 和
nth_element 都不是稳定的,但算法 stable_sort 可以保证这一点。
4 partition
partition 可以将对 Sequence Container 进行排序,排序完成后返回的迭代器指向满足条件的最后一个元素:
bool HasAcceptableQuality(const Widget& w); vector<Widget>::iterator goodEnd =
partition(widgets.begin(),
widgets.end(),
HasAcceptableQuality);
当上述表达式完成之后,返回的 goodEnd 指向了满足 HasAcceptableQuality 的最后一个元素,算法不保证 goodEnd 之前的元素顺序,但 stable_partition 可以保证 stability.
5 总结
- 需要对 vector, string, dequeu, array 进行完全排序 \(\rightarrow sort/stable\_sort\)
- 需要从 vector, string, dequeu, array 中获取排好序的前 N 个元素:\(\rightarrow partial\_sort\)
- 需要从 vector, string, dequeu, array 中获取无序的前 N 名或者指定的第 N 名: \(\rightarrow nth\_element\)
- 需要从顺序容器中获取符合某种条件的所有元素: \(\rightarrow partition/stable\_partition\)
- 需要对 List 进行排序,最好使用 List 本身的相关函数,或者将 List 转换成 vector 再使用 STL 算法。
上述各种算法的效率:
\(partition > stable\_partition > nth_element > partial\_sort > sort > stable\_sort\)
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <iterator>
#include <stdlib.h>
#include <sys/time.h> using namespace std; #define N 1000000 typedef unsigned int uint32; static inline uint32 current_time_ms()
{
struct timeval tv;
return (gettimeofday(&tv, NULL) != -1) ? \
tv.tv_sec * 1000 + tv.tv_usec / 1000 : 0;
} #define show(s,e) copy(s, e, os); cout << endl ; #define nth(v, N) *(v.begin()+N-1) bool PartitionCond(int& i)
{
return i < 50;
} int main(int argc, char *argv[])
{
srand(time(NULL));
vector<int> v(N);
for (int i = 0; i < N; ++i)
{
v[i] = i;
} random_shuffle(v.begin(), v.end()); ostream_iterator<int> os(cout, " "); cout << "Original array = " << endl;
show(v.begin(),v.begin()+21); cout << "Partial_sort for 20 elements... " << endl; vector<int> v1(v);
uint32 ts = current_time_ms(); partial_sort(v1.begin(), v1.begin()+21, v1.end(), less<int>());
cout << "time = " << current_time_ms() - ts << endl; show(v1.begin(),v1.begin()+21); cout << "nth_element of the 20th elements... " << endl;
vector<int> v2(v);
ts = current_time_ms();
nth_element(v2.begin(), v2.begin()+20, v2.end(),less<int>());
cout << "time = " << current_time_ms() - ts << endl;
show(v2.begin(),v2.begin()+21); cout << "v1[19] = " << nth(v1, 21) << endl;
cout << "v2[19] = " << nth(v2, 21) << endl; vector<int>v5(v);
sort(v5.begin(), v5.end()); if (nth(v5, 21) == nth(v2, 21))
{
cout << "nth_element works! " << endl;
}
else
{
cout << "nth_element is not working correctly! " << endl;
} cout << "Testing partition... " << endl;
vector<int> v3(v);
ts = current_time_ms();
vector<int>::iterator i1 = partition(v3.begin(), v3.end(), PartitionCond);
cout << "Time cost = " << current_time_ms() - ts << endl;
show(v3.begin(), i1); cout << "Testing stable_partition... " << endl;
vector<int> v4(v);
ts = current_time_ms();
vector<int>::iterator i2 = stable_partition(v4.begin(), v4.end(), PartitionCond);
cout << "Time cost = " << current_time_ms() - ts << endl;
show(v4.begin(), i2); return 0;
}
(转载请注明出处,使用许可:署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议 。)
Effective STL 学习笔记 31:排序算法的更多相关文章
- STL学习笔记(排序算法)
STL提供了好几种算法对区间内的元素排序.出来完全排序外,还支持局部排序. 对所有元素排序 void sort(RandomAccessIterator beg,RandomAccessIterato ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- Effective STL 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
- Effective STL 学习笔记:19 ~ 20
Effective STL 学习笔记:19 ~ 20 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value
Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...
- Effective STL 学习笔记 Item 30: 保证目标区间足够大
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- Effective STL 学习笔记: 多用 vector & string
Effective STL 学习笔记: 多用 vector & string 如果可能的话, 尽量避免自己去写动态分配的数组,转而使用 vector 和 string . 原书作者唯一想到的一 ...
随机推荐
- android studio 代码混淆如何忽略第三方jar包
日前在打包混淆包含第三方jar包的Android studio项目时 报出了各种错误,但是debug版本却能正常运行,于是怀疑android studio 打包的时候把第三方jar包给混淆了,第三方j ...
- Python如何引入自定义模块?
Python运行环境在查找库文件时是对 sys.path 列表进行遍历,如果我们想在运行环境中注册新的类库,主要有以下四种方法: 1.在sys.path列表中添加新的路径.这里可以在运行环境中直接修改 ...
- Java入门:基础算法之产生随机数
本程序演示使用Random类的呢想tInt()方法产生随机数. /* Program: 随机数发生器 * Written by: 理工云课堂 * Input: None * Output: 0 到20 ...
- docker日志引擎说明
docker原生支持众多的日志引擎,适用于各种不同的应用场景,本篇文档对其作一个简单的说明. Docker日志引擎说明 docker支持的日志引擎如下: none:关闭docker的回显日志, doc ...
- 题解 P2486 【[SDOI2011]染色】
写在前面 对于刚学树剖的同学比如我这种大大大蒟蒻来说,做这题会给你带来很大的提升:不仅可以对树剖有更深刻的理解,还可以更好的理解线段树,所以这是一道好题哦 为了更好懂,我一点一点说说思路吧 思路 首先 ...
- Hadoop生态圈-hive优化手段-作业和查询优化
Hadoop生态圈-hive优化手段-作业和查询优化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- python---基础知识回顾(十)进程和线程(协程gevent:线程在I/O请求上的优化)
优点:使用gevent协程,可以更好的利用线程资源.(基于线程实现) 需求:使用一个线程,去请求多个网站的资源(注意,请求上会有延时)<实际上是去请求了大量的网站信息,我们使用了多线程,只不过每 ...
- 设计模式之————依赖注入(Dependency Injection)与控制反转(Inversion of Controller)
参考链接: 依赖注入(DI) or 控制反转(IoC) laravel 学习笔记 —— 神奇的服务容器 PHP 依赖注入,从此不再考虑加载顺序 名词解释 IoC(Inversion of Contro ...
- RTSP服务器之————rtsp-server(轻量级RTSP / RTP流媒体服务器)
github:https://github.com/revmischa/rtsp-server 轻量级RTSP / RTP流媒体服务器
- 连接mysql提示Establishing SSL connection without server's identity verification is not recommended错误
Establishing SSL connection without server's identity verification is not recommended. According to ...