C++ STL 排序函数
2016-03-31 16:19
375 查看
C++的写法:
sort和stable_sort可对原生数组、STL容器排序,sort采用快排实现,不稳定,stable_sort采用归并排序实现,稳定,时间复杂度均为N*log(N)
C的写法:
qsort只能对原生数组排序,不能对STL容器排序
sort和stable_sort可对原生数组、STL容器排序,sort采用快排实现,不稳定,stable_sort采用归并排序实现,稳定,时间复杂度均为N*log(N)
//一般排序 //sort stable_sort在<algorithm>中 //函数原型 void sort(_RanIt _First, _RanIt _Last, _Pr _Pred) //参考 http://www.cplusplus.com/reference/algorithm/sort/ #include <string> #include <vector> #include <algorithm> #include <iostream> using namespace std; template <typename t> void Print (vector<t> array) { int size = array.size(); cout << "size: " << size << endl; for (int i = 0; i < size; ++i) { cout << array[i] << endl; } } bool CompIntLess(int first,int second) { return (first) < (second); } bool CompIntGreater(int first,int second) { return (first) > (second); } void SortInt() { vector<int> array; array.push_back(4); array.push_back(5); array.push_back(1); array.push_back(2); //排序 cout << "sort with from small to big" << endl; sort(array.begin(),array.end(),less<int>()); Print(array); cout << "sort with from big to small" << endl; sort(array.begin(),array.end(),greater<int>()); Print(array); //稳定排序 cout << "sort with from small to big" << endl; stable_sort(array.begin(),array.end(),less<int>()); Print(array); cout << "sort with from big to small" << endl; stable_sort(array.begin(),array.end(),greater<int>()); Print(array); //自写compare函数排序 cout << "sort with from small to big" << endl; sort(array.begin(),array.end(),CompIntLess); Print(array); cout << "sort with from big to small" << endl; sort(array.begin(),array.end(),CompIntGreater); Print(array); } void SortIntArray() { int a[10] = {5, 6, 4, 3, 7, 0 ,8, 9, 2, 1}; cout << "sort with from small to big" << endl; sort(a, a + 10, less<int>()); for (int i = 0; i < 10; i++) cout << a[i] << " " << endl; cout << "sort with from big to small" << endl; sort(a, a + 10, greater<int>()); for (int i = 0; i < 10; i++) cout << a[i] << " " << endl; } int main(){ SortInt(); SortIntArray(); int ttt = 0; return 0; }
C的写法:
qsort只能对原生数组排序,不能对STL容器排序
//快速排序 //qsort在<stdlib.h>中 //函数原型 void qsort(void * _Base, int _NumOfElements, int _SizeOfElements, int (* _PtFuncCompare)(const void *, const void *)); //参考 http://www.cplusplus.com/reference/cstdlib/qsort/ #include <iostream> #include <stdlib.h> using namespace std; int compare(const void *a, const void *b) { int *pa = (int*)a; int *pb = (int*)b; return (*pa) - (*pb); //从小到大排序 } void main() { int a[10] = {5, 6, 4, 3, 7, 0 ,8, 9, 2, 1}; qsort(a, 10, sizeof(int), compare); for (int i = 0; i < 10; i++) cout << a[i] << " " << endl; int ttt = 0; }
相关文章推荐
- 蘑菇街笔试题5
- C++ string与int的互相转换
- [高质量C++/C编程]—代码风格总结 未完结
- c++应用程序文件的编译过程
- C++STL库之algorithm库
- C++ STL 容器、迭代器、适配器
- 主要几个发行版的 gcc 对 c++ 新标准的支持情况区别
- 用C++实现一个Brainfuck解释器
- C语言中以十六进制输出字符型变量会出现'ffffff"的问题
- 左值和右值/auto ptr智能指针
- C++继承与派生上机记录
- 在vs2010中编译log4cxx-0.10.0详细方法(从下载、编译、解决错误详细介绍)
- C++日志系统log4cxx使用总结
- C++ 读写文件方法--fstream,ofstream,ifstream
- C++学习基础
- C++:只用初始化列表初始化变量的几种情况
- 简单常用滤波算法C语言实现
- C++中的虚函数
- C++------------------------------------------------------------- 排序
- VS项目属性中C/C++运行库 、MT /MTd /MD /MDd