哈希表排序O(m+n)

#include<iostream>
using namespace std;
const int maxn = 1000;
int main()
{
    int nums[10] = { 12, 88, 66, 122, 43, 66, 88, 99, 666, 888};
    int hash_map[maxn]={0};
    for(int i = 0; i < 10; i++)
    {
        hash_map[nums[i]]++;
    }
    for(int i = 0; i < maxn; i++)
    {
        for(int j = 0; j < hash_map[i]; j++)
        {
            cout<<i<<" ";
        }
    }
    cout<<endl;
    return 0;
}

哈希表排序O(m+n)
哈希表排序优点:时间复杂度为O(表长+n) n为元素个数.
缺点:只能对正整数排序,且哈希表长度必须大于最大待排序数字.

猜你喜欢

转载自blog.51cto.com/14472348/2474199