STL提供各种排序函数
sort采用的是成熟的“快速排序算法
stable_sort采用的是“归并排序”,其算法复杂度为n*log(n), 其优点是稳定
局部排序:
partial_sort采用的堆排序(heapsort),它在任何情况下的复杂度都是n*log(n).
nth_element 指定元素排序
从效率上看,以下几种sort算法的是一个排序,效率由高到低(耗时由小变大):
partion
stable_partition
nth_element
partial_sort
sort
stable_sort
排序算法的选择:
若需对vector, string, deque,容器进行全排序,你可选择sort或stable_sort;
若只需对vector, string, deque容器中取得top n的元素,部分排序partial_sort是首选.
若对于vector, string, deque容器,你需要找到第n个位置的元素或者你需要得到top n且不关系top n中的内部顺序,nth_element是最理想的;
若你需要从标准序列容器中把满足某个条件或者不满足某个条件的元素分开,你最好使用partition或
其实在STL还有make_heap, sort_heap等排序算法。
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
Sort的应用举例
默认的sort用法:
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
void main(){
int a[]={3, 1,4,2,5};
int len=sizeof(a)/sizeof(int);
sort(a,a+len); //默认:内置类型的由小到大排序
for (int i=0; i<len; i++)
cout<<a[i]<<'/t';
cout<<endl;
}
自定义的sort排序:
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
class myclass {
public:
myclass(int a, int b):first(a), second(b){}
int first;
int second;
bool operator < (const myclass &m)const {
return first < m.first;
}
};
bool less_second(const myclass & m1, const myclass & m2) {
return m1.second < m2.second;
}
int main() {
vector< myclass > vect;
for(int i = 0 ; i < 10 ; i ++){
myclass my(10-i, i*3);
vect.push_back(my);
}
for( i = 0 ; i < vect.size(); i ++)
cout<<"("<<vect[i].first<<","<<vect[i].second<<")/n";
sort(vect.begin(), vect.end());
cout<<"after sorted by first:"<<endl;
for( i = 0 ; i < vect.size(); i ++)
cout<<"("<<vect[i].first<<","<<vect[i].second<<")/n";
cout<<"after sorted by second:"<<endl;
sort(vect.begin(), vect.end(), less_second);
for( i = 0 ; i < vect.size(); i ++)
cout<<"("<<vect[i].first<<","<<vect[i].second<<")/n";
return 0 ;
}
Stable—sort应用举例:稳定排序
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
using namespace std;
class student{
public:
student(const string &a, int b):name(a), score(b){}
string name;
int score;
bool operator < (const student &m)const {
return score< m.score;
}
};
int main() {
vector< student> vect;
student st1("Tom", 74);
vect.push_back(st1);
st1.name="Jimy";
st1.score=56;
vect.push_back(st1);
st1.name="Mary";
st1.score=92;
vect.push_back(st1);
st1.name="Jessy";
st1.score=85;
vect.push_back(st1);
st1.name="Jone";
st1.score=56;
vect.push_back(st1);
st1.name="Bush";
st1.score=52;
vect.push_back(st1);
st1.name="Winter";
st1.score=77;
vect.push_back(st1);
cout<<"------before sort..."<<endl;
for(int i = 0 ; i < vect.size(); i ++) cout<<vect[i].name<<":/t"<<vect[i].score<<endl;
stable_sort(vect.begin(), vect.end());
//sort(vect.begin(), vect.end());
cout <<"-----after sort ...."<<endl;
for(i = 0 ; i < vect.size(); i ++) cout<<vect[i].name<<":/t"<<vect[i].score<<endl;
return 0 ;
}
其他排序:
cout<<"------before sort..."<<endl;
for(int i = 0 ; i < vect.size(); i ++) cout<<vect[i].name<<":/t"<<vect[i].score<<endl;
stable_sort(vect.begin(), vect.end());
partial_sort(vect.begin(), vect.begin()+3, vect.end()); //部分排序,取前三
student exam("pass", 60);
partition(vect.begin(), vect.end(), bind2nd(less<student>(), exam));
nth_element(vect.begin(), vect.begin()+3, vect.end());
cout <<"-----after sort ...."<<endl;
for(i = 0 ; i < vect.size(); i ++) cout<<vect[i].name<<":/t"<<vect[i].score<<endl;
return 0 ;