剑指offer 例题

题目:
实现一个排序算法,排序对象是本公司员工的年龄,要求时间复杂度O(n),空间复杂度不能超过O(n)。

#include<iostream>
using namespace std;
void SortAge(int Ages[],int length)
{
    if (NULL == Ages || length <= 0)
        return;
    const int oldAge = 99;
    int timesOfAge[oldAge+1];
    for (int i = 0; i <= oldAge; i++)
        timesOfAge[i] = 0;
    for (int j = 0; j < length;j++)
    {
        int age = Ages[j];
        if (age>oldAge || age < 0)
            throw exception("Invalid Value!");
        timesOfAge[age]++;
    }
    int index = 0;
    for (int k = 0; k <= oldAge; k++)
    {
        for (int s = 0; s < timesOfAge[k]; s++)
        {
            Ages[index] = k;
            index++;
        }
    }


}

int main()
{
    int Age[] = { 1, 5, 45, 56, 8, 6, 78, 5, 2, 4, 6, 23, 66, 65, 26, 23 };
    SortAge(Age,16);
    for (size_t i = 0; i < 16; i++)
    {
        cout << Age[i] << " ";
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值