场景:
1. C/C++的algorithm里提供的算法一般是集合的排序,查询和修改。
2. 也只有在特定场景在会用到以下算法函数.
代码: test_sort.cpp
#include <stdlib.h> #include <time.h> #include <assert.h> #include <algorithm> #include <iostream> #include <vector> using namespace std; // sort // stable_sort // partial_sort // reverse // sort_heap // nth_element // qsort //参考:http://www.cplusplus.com/reference/algorithm/is_sorted/ template <class ForwardIterator> bool is_sorted (ForwardIterator first, ForwardIterator last) { if (first==last) return true; ForwardIterator next = first; while (++next!=last) { if (*next<*first) // or, if (comp(*next,*first)) for version (2) return false; ++first; } return true; } const int kOrderNumber = 100000; template<class TYPE> struct StoreX : public unary_function<TYPE, void> { void operator() (TYPE& x) { *(&x) = rand(); } }; template<class TYPE> struct PrintX : public unary_function<TYPE, void> { void operator() (TYPE& x) { cout << "x: " << x << endl; } }; int CompareX(const void *a, const void *b) { int *a1 = (int*)a; int *b1 = (int*)b; if(*a1 < *b1) { return -1; }else if(*a1 == *b1) { return 0; }else { return 1; } } void TestQsort() { //C标准库的实现,最好情况:O(nlogn),2为底.最坏情况:n^2 int *ra = (int*)malloc(kOrderNumber*sizeof(int)); for_each( ra, ra + kOrderNumber, StoreX<int>() ); double time=clock(); qsort(ra,kOrderNumber,sizeof(int),CompareX); double Inteval = clock()-time; cout << "array elapse unit(mm): " << Inteval << endl; //for_each( ra, ra + kOrderNumber, PrintX<int>()); //C++的算法库没有快速排序的函数,需要自己实现. //参考: http://blog.csdn.net/infoworld/article/details/8537107 assert(is_sorted(ra,ra+kOrderNumber)); } void TestReverse() { //1.反转序列,时间复杂度K*N,fast std::vector<int> v(kOrderNumber); for_each( v.begin(), v.end(), StoreX<int>() ); int second = v[1]; int third = v[2]; cout << "second: " << second << ":" << "third: " << third << endl; double time=clock(); reverse(v.begin(),v.end()); double Inteval = clock()-time; cout << "vector elapse unit(mm): " << Inteval << endl; assert(v[kOrderNumber-2] == second); assert(v[kOrderNumber-3] == third); } void TestSort() { //Introsort:内省排序.时间复杂度:O(N log(N)) //http://zh.wikipedia.org/wiki/Introsort //如果遇到相等的元素,它们的先后顺序是不确定的. std::vector<int> v(kOrderNumber); for_each( v.begin(), v.end(), StoreX<int>() ); double time=clock(); sort(v.begin(),v.end()); double Inteval = clock()-time; cout << "sort vector elapse unit(mm): " << Inteval << endl; assert(is_sorted(v.begin(),v.end())); //for_each( ra, ra + kOrderNumber, PrintX<int>()); //sort_stable,如果遇到相等的元素,他们的顺序保持不变 //在最坏的情况下时间复杂度: N (log N)^2 instead of N log N. for_each( v.begin(), v.end(), StoreX<int>() ); time=clock(); stable_sort(v.begin(),v.end()); Inteval = clock()-time; cout << "stable_sort vector elapse unit(mm): " << Inteval << endl; assert(is_sorted(v.begin(),v.end())); //partial_sort:部分排序,start到middle之间的元素降序排列 for_each( v.begin(), v.end(), StoreX<int>() ); time=clock(); partial_sort(v.begin(),v.begin()+10000,v.end()); Inteval = clock()-time; cout << "partial_sort vector 10000 element,elapse unit(mm): " << Inteval << endl; assert(is_sorted(v.begin(),v.begin()+10000)); } void TestNth_element() { //1.nth_element,排序指定的某个元素,其他元素不管.比如获取降序排列后第6个元素可以快速得到. //2.能想到的应用场景就是在一堆乱序的对象里快速获取指定排名的对象. //时间复杂度:K*N std::vector<int> v; for (int i = 0; i < 10; ++i) { v.push_back(i); } //1.随机排序内部元素 std::random_shuffle(v.begin(),v.end()); for_each( v.begin(), v.end(), PrintX<int>() ); double time=clock(); //1.第6个元素 nth_element(v.begin(),v.begin()+5,v.end()); double Inteval = clock()-time; cout << "Nth_element vector elapse unit(mm): " << Inteval << endl; assert(*(v.begin()+5)==5); } int main(int argc, char const *argv[]) { srand( time(NULL) ); //1.多试几次验证排序正确性. for (int i = 0; i < 50; ++i) { cout << "TestQsort.............." << endl; TestQsort(); cout << "TestSort.............." << endl; TestSort(); cout << "TestReverse.............." << endl; TestReverse(); cout << "TestNth_element.............." << endl; TestNth_element(); } return 0; }
TestQsort.............. array elapse unit(mm): 31 TestSort.............. sort vector elapse unit(mm): 31 stable_sort vector elapse unit(mm): 31 partial_sort vector 10000 element,elapse unit(mm): 16 TestReverse.............. second: 27641:third: 8235 vector elapse unit(mm): 16 TestNth_element.............. x: 2 x: 5 x: 9 x: 7 x: 8 x: 0 x: 6 x: 3 x: 1 x: 4 Nth_element vector elapse unit(mm): 0 [Finished in 0.3s]