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学习笔记(排序算法)的更多相关文章

  1. STL学习笔记--排序算法

    排序算法 C++ STL 的排序算法(Sorting algorithms)是一组将无序序列排列成有序序列的模板函数或与排序相关的模板函数,提供了排序.折半搜索.归并.集合操作.堆操作.最值求解.字典 ...

  2. Java学习笔记——排序算法之快速排序

    会当凌绝顶,一览众山小. --望岳 如果说有哪个排序算法不能不会,那就是快速排序(Quick Sort)了 快速排序简单而高效,是最适合学习的进阶排序算法. 直接上代码: public class Q ...

  3. STL学习笔记(算法概述)

    算法头文件 要运用C++标准程序库的算法,首先必须包含头文件<algorithm> 使用STL算法时,经常需要用到仿函数以及函数配接器.它们定义域<functional>头文件 ...

  4. Java学习笔记——排序算法之希尔排序(Shell Sort)

    落日楼头,断鸿声里,江南游子.把吴钩看了,栏杆拍遍,无人会,登临意. --水龙吟·登建康赏心亭 希尔算法是希尔(D.L.Shell)于1959年提出的一种排序算法.是第一个时间复杂度突破O(n²)的算 ...

  5. STL学习笔记(五) 算法

    条款30:确保目标区间足够大 条款31:了解各种与排序有关的选择 //使用unaryPred划分输入序列,使得unaryPred为真的元素放在序列开头 partition(beg, end, unar ...

  6. Java学习笔记——排序算法之进阶排序(堆排序与分治并归排序)

    春蚕到死丝方尽,蜡炬成灰泪始干 --无题 这里介绍两个比较难的算法: 1.堆排序 2.分治并归排序 先说堆. 这里请大家先自行了解完全二叉树的数据结构. 堆是完全二叉树.大顶堆是在堆中,任意双亲值都大 ...

  7. Java学习笔记——排序算法之简单排序

    男儿何不带吴钩,收取关山五十州.请君暂上凌烟阁,若个书生万户侯? --南园十三首 三种排序法: 1.冒泡法 2.简单选择法 3.直接插入法   上代码: 1.冒泡排序 public class Bub ...

  8. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  9. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  10. Effective STL 学习笔记 Item 21:Comparison Function 相关

    Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...

随机推荐

  1. c/c++: c++函数返回类型什么情况带const

    c++ 函数的返回类型,包括const 什么时候起作用呢? 函数返回值不想其立即修改的. 例子如下,这是一个简单的避免产生隐形返回变量的方法,abc 的函数返回是引用,main函数中第10行,++ 操 ...

  2. 培训补坑(day4:网络流建模与二分图匹配)

    补坑时间到QAQ 好吧今天讲的是网络流建模与二分图匹配... day3的网络流建模好像说的差不多了.(囧) 那就接着补点吧.. 既然昨天讲了建图思想,那今天就讲讲网络流最重要的技巧:拆点. 拆点,顾名 ...

  3. tcpreplay 流量拆分算法研究

    1.1  算法目的 现在网络架构一般是Client-Server架构,所以网络流量一般是分 C-S 和 S-C 两个方向.tcpdump等抓包工具获取的pcap包,两个流向的数据没有被区分.流量方向的 ...

  4. 关闭vs警告

    禁用所有编译器警告 当“解决方案资源管理器”中有项目选中时,在“项目”菜单上单击“属性”. 单击“编译”选项卡. 选中“禁用所有警告”复选框. 禁用单个编译器警告 在“解决方案资源管理器”中选定一个项 ...

  5. 使用bottle进行web开发(1):hello world

    为什么使用bottle?因为简单,就一个py文件,和其他模块没有依赖,3000多行代码. http://www.bottlepy.org/docs/dev/ 既然开始学习,就安装它吧. pip3 in ...

  6. 用原生javascript实现最简单的发布者-订阅者模式

    http://www.cnblogs.com/surahe/p/6065778.html 发布—订阅模式可以广泛应用于异步编程中,这是一种替代传递回调函数的方案.比如,我们可以订阅 ajax 请求的 ...

  7. Nginx配置文件分析

    #user nobody; #启动进程数,即启动ngnix服务的个数,通常设置和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/e ...

  8. [POI2006]Periods of Words

    题目大意: 给定一个长度为$n(n\leq10^6)$的字符串$S$,定义一个串$S$的最大周期为一个不为$S$的字符串$Q$,满足$Q$为$S$的前缀且$S$为$QQ$的前缀.求字符串$S$的每一个 ...

  9. Oracle、SQLServer、ArcSDE怎么查看版本、补丁

    http://blog.csdn.net/linghe301/article/details/6712544

  10. 在React组件unmounted之后setState的报错处理

    最近在做项目的时候遇到一个问题,在 react 组件 unmounted 之后 setState 会报错.我们先来看个例子, 重现一下问题: class Welcome extends Compone ...