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. error LNK2001: 无法解析的外部符号 "public: virtual long __stdcall CBaseFilter(转)

    原文转自 https://www.cnblogs.com/xiongjiaji/archive/2010/12/31/2476565.html 今天用VS2005编译DirectShow程序,发现出来 ...

  2. (转)vim 常用快捷键 二

    转自:http://www.cnblogs.com/wangkangluo1/archive/2012/04/12/2444952.html 键盘移动 (Move) 一切都从键盘的移动 k -> ...

  3. NS_AVAILABLE_IOS(6_0)

    http://www.cocoachina.com/bbs/read.php?tid=241951 一个简单的小问题,请诸位大侠帮助给看看 ,新手 ,勿拍砖       本帖属于CocoaChina会 ...

  4. mysql-MHA 故障收集

    在manager 主机上开启监控服务,启动不了 [root@manager ~]# managerStart [] [root@manager ~]# managerStatus app1 is st ...

  5. Android 各种功能代码收集

    1.分享图片等文件到单个指定微信好友 /** * 分享信息到朋友 * * @param file * 假如图片的路径为path,那么file = new File(path); */ private ...

  6. 读取EXCEL的办法

    private void button9_Click(object sender, EventArgs e) { var folder =new FolderBrowserDialog(); if ( ...

  7. 正则表达式之Regex.Replace()用法

    正则表达式替换匹配到的字符串 string txt = "AAA12345678AAAA"; //匹配到的连续数字的前4位用*替换 string m =Regex.Replace( ...

  8. AC日记——算术天才⑨与等差数列 bzoj 4373

    4373 思路: 判断一个数列是否是等差数列: 1,最大值减去最小值==(区间个数-1)*k: 2,gcd==k: 3,不能有重复(不会这判断这一条,但是数据水就过了): 来,上代码: #includ ...

  9. kibana- Timelion

    1. Visualize 新建图形 2. 选择图形类型 3. 选择索引 4. 设置Timelion表达式 5. 保存图形

  10. Xamarin XAML语言教程构建进度条ProgressBar

    Xamarin XAML语言教程构建进度条ProgressBar Xamarin XAML语言教程构建进度条ProgressBar,ProgressBar被称为进度条,它类似于没有滑块的滑块控件.进度 ...