小谈C++STL算法-sorting

C++STL也提供了排序算法(Sorting algorithms),排序就是将一组无序的序列变成有序的序列.

首先介绍一下关于堆排序的内容,下面就是关于堆排序的一些算法函数

push_heap

元素入堆,就是将一个元素置入已构成的堆的迭代器区间中,使得扩展的一个元素的区间元素仍构成堆.(会改变迭代器区间元素顺序)

make_heap

重新排列元素顺序,使得他们在逻辑上构成一个堆.

pop_heap

相对于push_heap,实际上就是元素出堆操作

sort_heap

利用堆进行排序

is_heap

判断迭代器区间是否构成一个堆

partial_sort

堆部分函数进行排序,内部也是使用堆排序.(给定一个迭代器区间和区间内的一个元素,对该元素前面的区间排序,)

partial_sort_copy

类似于partial_sort,只是它是将结果放置到另一个区间范围中.

----

不过一定程度上堆排序的速度并不算快.

sort

STL当中比较常用的一个排序算法,较之堆排序和标准Cqsort算法都要快.

下面的代码展示了如何使用sort实现序列的升降序排列

#include <algorithm>

#include <iostream>

using namespace std;

void print(int x){

       cout << x << ' ';

}

int main(void){

       int iArray[]={2, 8, -15, 90, 26, 7, 23, 30, -27, 39, 55};

       const int len=sizeof(iArray)/sizeof(int);

       //

       cout << "升序排序" << endl;

       sort(iArray, iArray+len);

       for_each(iArray, iArray+len, print);

       cout << endl;

       //

       cout << "降序排序" << endl;

       sort(iArray, iArray+len, greater<int>());

       for_each(iArray, iArray+len, print);

       cout << endl;

       return 0;

}

merge

将两个相同升降序的序列合并成一个有序的序列

inplace_merge

类似于merge,用于将一个序列内部两个相同升降序的子序列进行排序合并

stable_sort

类似于sort,不过该算法可以保持等价元素的相对顺序稳定不变

下面的代码展示了在对于结构体排序的实用性:

#include <algorithm>

#include <vector>

#include <iostream>

using namespace std;

struct Student{

       int id;

       char* name;

       int score;

       Student(int id_, char* name_, int score_){

              id=id_;

              name=name_;

              score=score_;

       }

};

bool compByscore(Student s1, Student s2){

       return s1.score < s2.score ? 1 : 0;

}    

void print(Student s){

       cout << s.id << ' ' << s.name << ' ' << s.score << endl;

}

int main(void){

       vector<Student> v;

       v.push_back(Student(5, "李强", 90));

       v.push_back(Student(9, "王文", 80));

       v.push_back(Student(8, "张天", 87));

       v.push_back(Student(6, "丁宏", 90));

       v.push_back(Student(7, "赵庆", 99));

       //

       stable_sort(v.begin(), v.end(), compByscore);

       for_each(v.begin(), v.end(), print);

       cout << endl;

       return 0;

}

is_sorted

判断迭代器区间内的元素是否排序

nth_element

仅排序第nth个元素,就是使得该元素前面的元素小于等于(或者大于等于)该元素,后面的元素大于等于(或者小于等于该元素)

如果不理解的话,就参考下面代码:

#include <algorithm>

#include <iostream>

using namespace std;

void print(int x){

       cout << x << ',';

}

int main(void){

       int iArray[]={7, 8, 6, 2, 9, 5, 10, 3, 0, 1, 13, 11, 12};

       const int len=sizeof(iArray)/sizeof(int);

       //

       for_each(iArray, iArray + len, print);

       cout << endl;

       //

       cout << "After nth_element 9th:\n";

       nth_element(iArray, iArray + 9, iArray + len);

       for_each(iArray, iArray + len, print);

       cout << endl;

       return 0;

}

lower_bound

查找有序区间中首个不小于指定值的元素.

upper_bound

对应于lower_bound, 查找有序区间中首个不大于指定值的元素.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值