STL学习笔记(排序算法)
STL提供了好几种算法对区间内的元素排序。出来完全排序外,还支持局部排序。
对所有元素排序
void
sort(RandomAccessIterator beg,RandomAccessIterator end)
void
sort(RandomAccessIterator beg,RandomAccessIteratro end,
BinaryPredicate op)
void
stable_sort(RandomAccessIterator beg,RandomAccessIterator end)
void
stable_sort(RandomAccessIterator beg,RandomAccessIterator end,
BinaryPredicate op)
1.sort()和stable_sort()的上述第一形式,使用operator<对区间[beg,end)内的所有元素进行排序
2.sort()和stable_sort()的上述第二形式,使用判断式op(elem1,elem2)作为排序准则,对区间[beg,end)内的所有元素进行排序
3.sort()和stable_sort()的区别是,后者保证相等元素的原本相对次序在排序后保持不变。
下面这个例子示范sort()的用法
#include "algostuff.hpp"
using namespace std; int main()
{
deque<int> coll;
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll,"on entry: ");
sort(coll.begin(),coll.end());
PRINT_ELEMENTS(coll,"sorted: ");
sort(coll.begin(),coll.end(),greater<int>());
PRINT_ELEMENTS(coll,"sorted >:");
}
以下程序示范sort()和stable_sort()两者间的区别
#include "algostuff.hpp"
using namespace std; bool lessLength(const string& s1,const string& s2)
{
return s1.length() < s2.length();
} int main()
{
vector<string> coll1;
vector<string> coll2;
coll1.push_back("1xxx");
coll1.push_back("2x");
coll1.push_back("3x");
coll1.push_back("4x");
coll1.push_back("5xx");
coll1.push_back("6xxxx");
coll1.push_back("7xx");
coll1.push_back("8xxx");
coll1.push_back("9xx");
coll1.push_back("10xxx");
coll1.push_back("");
coll1.push_back("");
coll1.push_back("");
coll1.push_back("14xx");
coll1.push_back("");
coll1.push_back("");
coll1.push_back("");
coll2=coll1;
PRINT_ELEMENTS(coll1,"on entry:\n");
sort(coll1.begin(),coll1.end(),lessLength);
stable_sort(coll2.begin(),coll2.end(),lessLength);
PRINT_ELEMENTS(coll1,"\nwith sort():\n");
PRINT_ELEMENTS(coll2,"\nwith stable_sort():\n");
}
局部排序
void
partial_sort(RandomAccessIterator beg,
RandomAccessIterator sortEnd,
RandomAccessIterator end)
void
partial_sort(RandomAccessIterator beg,
RandomAccessIterator sortEnd,
RandomAccessIterator end,
BinaryPredicate op)
1.以上第一形式,以operator<对区间[beg,end)内的元素进行排序,是区间[beg,end)内的元素处于有序状态。
2.以上第二形式,运用二元判断式: op(elem1,elem2)进行局部排序。
以下程序示范partial_sort()的用法
#include "algostuff.hpp"
using namespace std; int main()
{
deque<int> coll;
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll);
partial_sort(coll.begin(),coll.begin()+,coll.end());
PRINT_ELEMENTS(coll);
partial_sort(coll.begin(),coll.begin()+,coll.end(),greater<int>());
PRINT_ELEMENTS(coll);
partial_sort(coll.begin(),coll.end(),coll.end());
PRINT_ELEMENTS(coll);
}
RandomAccessIterator
partital_sort_copy(InputIterator sourceBeg,
InputIterator sourceEnd,
RandomAccessIterator destBeg,
RandomAccessIterator destEnd)
RandomAccessIterator
partial_sort_copy(InputIterator sourceBeg,
InputIterator sourceEnd,
RandomAccessIterator destBeg,
RandomAccessIterator destEnd,
BinaryPredicate op)
1.两者都是copy()和partial_sort()的组合
2.它们将元素从源区间[sourceBeg,sourceEnd)复制到目标区间[destBeg,destEnd),同时进行排序
3.被排序(被复制)的元素数量是源区间和目标区间两者所含元素数量的较少值
以下程序示范partial_sort_copy()的用法
#include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
deque<int> coll1;
vector<int> coll6();
vector<int> coll30();
INSERT_ELEMENTS(coll1,,);
INSERT_ELEMENTS(coll1,,);
INSERT_ELEMENTS(coll1,,);
PRINT_ELEMENTS(coll1);
vector<int>::iterator pos6;
pos6=partial_sort_copy(coll1.begin(),coll1.end(),coll6.begin(),coll6.end());
copy(coll6.begin(),pos6,ostream_iterator<int>(cout," "));
cout<<endl;
vector<int>::iterator pos30;
pos30=partial_sort_copy(coll1.begin(),coll1.end(),coll30.begin(),coll30.end(),greater<int>());
copy(coll30.begin(),pos30,ostream_iterator<int>(cout," "));
cout<<endl;
}
根据第n个元素排序
void
nth_element(RandomAccessIterator beg,
RandomAccessIterator nth,
RandomAccessIterator end)
void
nth_element(RandomAccessIterator beg,
RandomAccessIterator nth,
RaddomAccessIterator end,
BinaryPredicate op)
1.两种形式都对区间[beg,end)内的元素进行排序,使第n个位置上的元素就位。也就是说:
所有在位置n之前的元素都小于等于它,所有在位置n之后的元素都大于等于它。
以下程序示范nth_element()的用法
#include "algostuff.hpp"
#include <iterator>
using namespace std; int main()
{
deque<int> coll;
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll);
nth_element(coll.begin(),coll.begin()+,coll.end());
cout<<"the four lowest elements are: ";
copy(coll.begin(),coll.begin()+,ostream_iterator<int>(cout," "));
cout<<endl;
}
Heap算法
就排序而言,heap是一种特别的元素组织方式,应用于heap排序法(heapsort)。
heap可被视为一个以序列式群集视作而成的二叉树,具有两大性质:
1.第一个元素总是最大
2.总是能够在对数时间内增加或移除一个元素
关于heapsort,可以查看之前的博客:http://www.cnblogs.com/runnyu/p/4677170.html
为了处理heap,STL提供四种算法
1.make_heap() 将某区间内的元素转化成heap
2.push_heap() 对着heap增加一个元素
3.pop_heap() 对着heap取出下一个元素
4.sort_heap() 将heap转化为一个已序群集(此后它就不再是heap了)
heap算法细节
void
make_heap(RandomAccessIterator beg,
RandomAccessIterator end)
void
make_heap(RandomAccessIterator beg,
RandomAccessIterator end,
BinaryPredicate op)
两种形式都将区间[beg,end)内的元素转化为heap
void
push_heap(RandomAccessIterator beg,RandomAccessIterator end)
void
push_heap(RandomAccessIterator beg,RandomAccessIterator end,
BinaryPredicate op)
两种形式都将end之前的最后一个元素加入原本就是heap的[beg,end-1)区间内,是区间[beg,end)成为一个heap
void
pop_heap(RandomAccessIterator beg,RandomAccessIterator end)
void
pop_heap(RandomAccessIterator beg,RandomAccessIterator end,
BinaryPredicate op)
以上两种形式都将heap[beg,end)内的最高元素,也就是第一元素,移到最后位置,并将剩余区间[beg,end-1)内的元素组织起来成为一个新的heap
void
sort_heap(RandomAccessIterator beg,RandomAccessIterator end)
void
sort_heap(RandomAccessIterator beg,RandomAccessIterator end,
BinaryPredicate op)
两种形式都可以将heap[beg,end)转换为一个已序序列
以下程序示范如何使用各种heap算法
#include "algostuff.hpp"
using namespace std; int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll,"on entry: ");
make_heap(coll.begin(),coll.end());
PRINT_ELEMENTS(coll,"after make_heap(): ");
coll.push_back();
push_heap(coll.begin(),coll.end());
PRINT_ELEMENTS(coll,"after push_heap()");
sort_heap(coll.begin(),coll.end());
PRINT_ELEMENTS(coll,"after sort_heap(): ");
}
STL学习笔记(排序算法)的更多相关文章
- STL学习笔记--排序算法
排序算法 C++ STL 的排序算法(Sorting algorithms)是一组将无序序列排列成有序序列的模板函数或与排序相关的模板函数,提供了排序.折半搜索.归并.集合操作.堆操作.最值求解.字典 ...
- Java学习笔记——排序算法之快速排序
会当凌绝顶,一览众山小. --望岳 如果说有哪个排序算法不能不会,那就是快速排序(Quick Sort)了 快速排序简单而高效,是最适合学习的进阶排序算法. 直接上代码: public class Q ...
- STL学习笔记(算法概述)
算法头文件 要运用C++标准程序库的算法,首先必须包含头文件<algorithm> 使用STL算法时,经常需要用到仿函数以及函数配接器.它们定义域<functional>头文件 ...
- Java学习笔记——排序算法之希尔排序(Shell Sort)
落日楼头,断鸿声里,江南游子.把吴钩看了,栏杆拍遍,无人会,登临意. --水龙吟·登建康赏心亭 希尔算法是希尔(D.L.Shell)于1959年提出的一种排序算法.是第一个时间复杂度突破O(n²)的算 ...
- STL学习笔记(五) 算法
条款30:确保目标区间足够大 条款31:了解各种与排序有关的选择 //使用unaryPred划分输入序列,使得unaryPred为真的元素放在序列开头 partition(beg, end, unar ...
- Java学习笔记——排序算法之进阶排序(堆排序与分治并归排序)
春蚕到死丝方尽,蜡炬成灰泪始干 --无题 这里介绍两个比较难的算法: 1.堆排序 2.分治并归排序 先说堆. 这里请大家先自行了解完全二叉树的数据结构. 堆是完全二叉树.大顶堆是在堆中,任意双亲值都大 ...
- Java学习笔记——排序算法之简单排序
男儿何不带吴钩,收取关山五十州.请君暂上凌烟阁,若个书生万户侯? --南园十三首 三种排序法: 1.冒泡法 2.简单选择法 3.直接插入法 上代码: 1.冒泡排序 public class Bub ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- 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 ...
随机推荐
- 【调试】js调试console.log使用总结图解(重要)
0.介绍 先上图:不知道有多少人发现,在浏览器开发工具的“Console”上的百度首页的关于百度招聘的信息: 今天要给大家介绍的就是是Web前端调试工具中的Console面板,应该说只要是个浏览器就会 ...
- WPF中的URI
在 Windows Presentation Foundation (WPF) 中,使用统一资源标识符 (URI) 标识和加载文件的方式有很多,包括: 指定当应用程序第一次启动时显示的用户界面 (UI ...
- centos 安装squid http代理
yum -y install squid service squid start service iptables stop
- Cocos2D研究院之CCNode详解(三)
http://www.xuanyusong.com/archives/950 上一章我们了解了cocos2d的项目路径以及工作原理,这次作者要真刀真枪地讲解代码了,咱们先来看看cocos2d最常用.也 ...
- 包嗅探和包回放简介-tcpdump,tcpreplay
一. 嗅探 1.1 嗅探技术简介 1.1.1 目标 嗅探的目标:获取在网络上传输的各种有价值信息:账号.密码.非公开协议 1.1.2 原理 嗅探的原理:大多数嗅探都是在以太网内,利用数据链路层 ...
- Webpack & The Hot Module Replacement热模块替换原理解析
Webpack & The Hot Module Replacement热模块替换原理解析 The Hot Module Replacement(HMR)俗称热模块替换.主要用来当代码产生变化 ...
- 有个人想上一个n级的台阶,每次只能迈1级或者迈2级台阶,问:这个人有多少种方法可以把台阶走完?
有个人想上一个n级的台阶,每次只能迈1级或者迈2级台阶,问:这个人有多少种方法可以把台阶走完? 相关问题: (1)有个人想上一个n级的台阶,每次只能迈1级或者迈2级台阶,问:这个人有多少种方法可以把台 ...
- 【C语言】复杂类型声明
原文地址: http://blog.csdn.net/wangweixaut061/article/details/6549768 原文不让转载,但实在是有用,就拷贝了一小部分过来.全文请点开链接. ...
- visio画任意形状图形
1,连接线--右击---曲线连接线 2,选中组合 3,开发工具--操作--连接--填充
- springBoot Ribbon 负载均衡
1.依赖引用 <!-- 引入关于 eureka-server的依赖 --> <dependency> <groupId>org.springframework.cl ...