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. 用MysQL语句怎么进行远程连接数据库

    一.连接远程数据库: 1.显示密码如:MySQL 连接远程数据库(192.168.2.115),端口“3306”,用户名为“root”,密码“root” C: -u root -proot (注意第一 ...

  2. API函数的学习与运用

    1.窗口 1.获取最前方的窗口句柄 GetForegroundWindow() 返回值:HWND类型. 调用方式:HWND hwnd=GetForegroundWindow();即hwnd就存着你的窗 ...

  3. 合唱队形(DP)

    原题传送门 这道题目就是裸的DP题, 我们所需要得到的是一个倒V形的数列 即一个上升子序列与下降子序列的合体.. 所以我们只需要做一遍从1到n的最长上升子序列和从n到1的最长上升子序列即可 时间复杂度 ...

  4. YYH的王国(NOIP模拟赛Round 6)

    题目描述 YYH拥有一个有n个城市的国家,编号为1~n.其中城市i到城市j的路径长度为i和j的最小公倍数.现在YYH想建一些高速公路,使得任意两座城市都能通过高速公路直接或间接到达.建造一条高速公路的 ...

  5. ES6 - Babel编译环境搭建

    都看到这里了,我就不写什么node环境安装之类的了. 直接从新建项目文件夹后开始吧! 安装依赖: 命令行cd到项目文件夹之后,执行以下命令:(mac记得前边加sudo) npm init –y  // ...

  6. python的优化机制与垃圾回收与gc模块

    python属于动态语言,我们可以随意的创建和销毁变量,如果频繁的创建和销毁则会浪费cpu,那么python内部是如何优化的呢? python和其他很多高级语言一样,都自带垃圾回收机制,不用我们去维护 ...

  7. 关于js拖拽功能,拖拽元素的position:fixed;left:0;right:0;样式引起左右拖动元素会出现落后鼠标移动距离的问题

    被拖拽元素的样式如果为:position:fixed;left:0;right:0;(当时是为了让fixed定位的元素水平居中加的left:0;right:0;避免js动态计算定位的麻烦)时左右拖动会 ...

  8. 全文索引CONTAINS语法

    Like直接在数据据中查找可以查到所有所需记录但是会扫描整个表会影响性能CONTAINS是基于全文索引进行查询,查询结果受系统全文索引分词的方法影响查询结果会不全.Select * FROM A Wh ...

  9. cpp调用dll

    c++ 显示调用 其他程序dll #pragma comment(lib, "DllTest.lib") int fnDllTest(void); int c = fnDllTes ...

  10. (2)ASP.NET 页面指令

    页面指令 一共有12个指令,这些指令用来控制APS.NET页面的行为. Assembly,Control,Implements,Import,Master,MasterTpye,OutputCache ...