STL学习笔记(排序算法)

本文深入探讨了C++ Standard Template Library (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()的用法

 1 #include "algostuff.hpp"
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     deque<int> coll;
 7     INSERT_ELEMENTS(coll,1,9);
 8     INSERT_ELEMENTS(coll,1,9);
 9     PRINT_ELEMENTS(coll,"on entry: ");
10     sort(coll.begin(),coll.end());
11     PRINT_ELEMENTS(coll,"sorted: ");
12     sort(coll.begin(),coll.end(),greater<int>());
13     PRINT_ELEMENTS(coll,"sorted >:"); 
14 }
View Code

以下程序示范sort()和stable_sort()两者间的区别

 1 #include "algostuff.hpp"
 2 using namespace std;
 3 
 4 bool lessLength(const string& s1,const string& s2)
 5 {
 6     return s1.length() < s2.length();
 7 }
 8 
 9 int main()
10 {
11     vector<string> coll1;
12     vector<string> coll2;
13     coll1.push_back("1xxx");
14     coll1.push_back("2x");
15     coll1.push_back("3x");
16     coll1.push_back("4x");
17     coll1.push_back("5xx");
18     coll1.push_back("6xxxx");
19     coll1.push_back("7xx");
20     coll1.push_back("8xxx");
21     coll1.push_back("9xx");
22     coll1.push_back("10xxx");
23     coll1.push_back("11");
24     coll1.push_back("12");
25     coll1.push_back("13");
26     coll1.push_back("14xx");
27     coll1.push_back("15");
28     coll1.push_back("16");
29     coll1.push_back("17");
30     coll2=coll1;
31     PRINT_ELEMENTS(coll1,"on entry:\n");
32     sort(coll1.begin(),coll1.end(),lessLength);
33     stable_sort(coll2.begin(),coll2.end(),lessLength);
34     PRINT_ELEMENTS(coll1,"\nwith sort():\n");
35     PRINT_ELEMENTS(coll2,"\nwith stable_sort():\n");
36 }
View Code

 

 

局部排序

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()的用法

 1 #include "algostuff.hpp"
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     deque<int> coll;
 7     INSERT_ELEMENTS(coll,3,7);
 8     INSERT_ELEMENTS(coll,2,6);
 9     INSERT_ELEMENTS(coll,1,5);
10     PRINT_ELEMENTS(coll);
11     partial_sort(coll.begin(),coll.begin()+5,coll.end());
12     PRINT_ELEMENTS(coll);
13     partial_sort(coll.begin(),coll.begin()+5,coll.end(),greater<int>());
14     PRINT_ELEMENTS(coll);
15     partial_sort(coll.begin(),coll.end(),coll.end());
16     PRINT_ELEMENTS(coll);
17 }
View Code

 

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()的用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     deque<int> coll1;
 8     vector<int> coll6(6);
 9     vector<int> coll30(30);
10     INSERT_ELEMENTS(coll1,3,7);
11     INSERT_ELEMENTS(coll1,2,6);
12     INSERT_ELEMENTS(coll1,1,5);
13     PRINT_ELEMENTS(coll1);
14     vector<int>::iterator pos6;
15     pos6=partial_sort_copy(coll1.begin(),coll1.end(),coll6.begin(),coll6.end());
16     copy(coll6.begin(),pos6,ostream_iterator<int>(cout," "));
17     cout<<endl;
18     vector<int>::iterator pos30;
19     pos30=partial_sort_copy(coll1.begin(),coll1.end(),coll30.begin(),coll30.end(),greater<int>());
20     copy(coll30.begin(),pos30,ostream_iterator<int>(cout," "));
21     cout<<endl;
22 }
View Code

 

 

根据第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()的用法

 1 #include "algostuff.hpp"
 2 #include <iterator>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     deque<int> coll;
 8     INSERT_ELEMENTS(coll,3,7);
 9     INSERT_ELEMENTS(coll,2,6);
10     INSERT_ELEMENTS(coll,1,5); 
11     PRINT_ELEMENTS(coll);
12     nth_element(coll.begin(),coll.begin()+3,coll.end());
13     cout<<"the four lowest elements are: ";
14     copy(coll.begin(),coll.begin()+4,ostream_iterator<int>(cout," "));
15     cout<<endl;
16 }
View Code

 

 

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算法

 1 #include "algostuff.hpp"
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     vector<int> coll;
 7     INSERT_ELEMENTS(coll,3,7);
 8     INSERT_ELEMENTS(coll,5,9);
 9     INSERT_ELEMENTS(coll,1,4);
10     PRINT_ELEMENTS(coll,"on entry: ");
11     make_heap(coll.begin(),coll.end());
12     PRINT_ELEMENTS(coll,"after make_heap(): ");
13     coll.push_back(17);
14     push_heap(coll.begin(),coll.end());
15     PRINT_ELEMENTS(coll,"after push_heap()");
16     sort_heap(coll.begin(),coll.end());
17     PRINT_ELEMENTS(coll,"after sort_heap(): ");
18 }
View Code

 

转载于:https://www.cnblogs.com/runnyu/p/4849503.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值