Sorting algorithm排序算法
Sort() 对所有元素排序
Stable_sort() 对所有元素排序,并保持相等元素间的相对次序
Partial_sort() 排序,至到有N个元素就位
Partitial_sort_copy() 排序,至到有N个元素就位,结果复制于它处
Nth_element() 根据第N个位置进行排序
Partition() 改变元素次序,使符合某规则者移到前面
Stable_partition() partition相似,但保持符合准则与不符合准则之各个元素之间的位置
Make_heap() 将一个区间转换成一个heap
Push_heap() 将元素加入一个heap
Pop_heap() 从heap移除一个元素
Sort_heap() 对heap进行排序,排序后不再是heap
-----------------------------------------
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
void print(const int &elem)
{
cout << elem << ' ';
}
int main()
{
vector<int> vec;
vec.push_back(2);
vec.push_back(10);
vec.push_back(5);
vec.push_back(13);
vec.push_back(7);
vec.push_back(13);
vec.push_back(11);
vec.push_back(8);
for_each(vec.begin(), vec.end(), print);
cout << endl;
partial_sort(vec.begin(), find(vec.begin(), vec.end(), 7), vec.end());
for_each(vec.begin(), vec.end(), print);
cout << endl;
random_shuffle(vec.begin(), vec.end());
for_each(vec.begin(), vec.end(), print);
cout << endl;
nth_element(vec.begin(), find(vec.begin(), vec.end(), 7), vec.end());
for_each(vec.begin(), vec.end(), print);
cout << endl;
return 0;
}
运行算法:
2 10 5 13 7 13 11 8
2 5 7 8 13 13 11 10
13 8 10 11 2 13 7 5
5 2 7 8 10 11 13 13