码迷,mamicode.com
首页 > 编程语言 > 详细

桶排序

时间:2015-12-24 23:45:19      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

  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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!