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. [置顶] Linux Malloc分析-从用户空间到内核空间【转】

    转自:http://blog.csdn.net/ordeder/article/details/41654509 版权声明:本文为博主(http://blog.csdn.net/ordeder)原创文 ...

  2. selenium访问百度 然后获取百度logo的截图

    #!/usr/bin/env python # encoding: utf-8 import time from selenium import webdriver from PIL import I ...

  3. 优化html中mp4视频加载速度

    如果使用参数faststart就会在生成完上边结构之后将moov移动到mdat前面:ffmpeg –i input.flv –c copy –f mp4 –movflags faststart out ...

  4. 在线查看PDF文档

    http://www.cnblogs.com/morang/p/4598894.html http://78re52.com1.z0.glb.clouddn.com/resource%2Fscenar ...

  5. vi / vim 设置

    一.vi下方向键输入后,出现ABCD,解决方法: 在vi中输入:set nocp 按回车即可. 二.设置TAB缩进4个空格: 为了vim更好的支持python写代码,修改tab默认4个空格有两种设置方 ...

  6. CF978E Bus Video System【数学/前缀和/思维】

    [链接]: CF [分析]: 设上车前人数 x ,中途最大人数为 x+max ,最小人数为 x+min (max≥0,min≤0) 可得不等式组 x+max≤w, x+min≥0 整数解个数为 max ...

  7. #424 Div2 C

    #424 Div2 C 题意 给出 k 个分数,初始分数未知,按顺序把这 k 个分数加到初始分数上,已知 n 个加入分数后的结果(无序),问初始分数有多少种可能. 分析 也就是说这 n 个结果,它们之 ...

  8. 洛谷——P1170 兔八哥与猎人

    P1170 兔八哥与猎人 题目描述 兔八哥躲藏在树林旁边的果园里.果园有M × N棵树,组成一个M行N列的矩阵,水平或垂直相邻的两棵树的距离为1.兔八哥在一棵果树下. 猎人背着猎枪走进了果园,他爬上一 ...

  9. codevs 1025 选菜——01背包

    时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 在小松宿舍楼下的不远处,有PK大学最不错的一个食堂—— ...

  10. sql with multiply where

    I am wondering if this is a valid query: UPDATE table SET ID = 111111259 WHERE ID = 2555 AND SET ID ...