标签:
C++实现的“桶排序”,采用了模板技术。底层数据结构是 std::map ,其本质是优先队列。
时间复杂度是O(M + N),其中 M 是数据范围的最大值,N 是数据量。额外的,当 M = O(N) 时,时间复杂度是 O(N)。
#include <iostream> #include <map> using namespace std; template<typename T> void bucketSorting(T* begin, T* end) { T* it = begin; map<T, unsigned> bucket; while (it != end) bucket[*it++]++; for (auto e : bucket) *begin++ = e.first; } int main() { int arr[] = { 5, 4, 30, 10, 100 }; bucketSorting(arr, (arr + 5)); for (auto e : arr) cout << e << " "; return 0; }
标签:
原文地址:http://www.cnblogs.com/fengyubo/p/5074568.html