剑指offer-公司员工年龄排序

/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
*******************************************************************/
// 作者:TyroneLi
//

#include <iostream>
#include <cstdio>

/*
Q:
    对一个公司的员工年龄进行排序,公司员工有几万人
S:
    (年龄数字的大小是在一个较小的范围之内,允许使用常量大小的空间,不得超过O(n))
    根据每个年龄数的出现次数进行统计,然后排序
*/

void sortAges(int ages[], int length)
{
    if(ages == nullptr || length <= 0)
        return;
    
    const int maxAge = 99;
    int timesOfAges[maxAge + 1];

    for(int i = 0; i <= maxAge; ++i)
        timesOfAges[i] = 0;
    
    for(int j = 0; j < length; ++j)
    {
        int age = *(ages + j); 
        if(age < 0 || age > maxAge)
        {
            std::cout << "age error! " << age << std::endl;
            return;
        }
        ++timesOfAges[age];
    }

    int index = 0;
    for(int i = 0; i <= maxAge; ++i)
    {
        for(int j = 0; j < timesOfAges[i]; ++j)
        {
            ages[index] = i;
            ++index;
        }
    }
}

void test_1()
{
    std::cout << "test 1" << std::endl;
    int ages[] = {28, 45, 89, 37, 72, 32, 42, 12, 52, 34, 22, 25, 26, 28, 19, 27, 24, 22, 42};
    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;

    sortAges(ages, 19);

    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_2()
{
    std::cout << "test 2" << std::endl;
    int ages[] = {0, 45, 89, 37, 72, 32, 42, 12, 52, 34, 22, 25, 26, 28, 19, 27, 24, 22, 42};
    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;

    sortAges(ages, 19);

    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_3()
{
    std::cout << "test 3" << std::endl;
    int ages[] = {0, 45, 89, 37, 72, 32, 42, 12, 52, 34, 22, 25, 26, 28, 19, 27, 24, 22, -1};
    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;

    sortAges(ages, 19);

    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_4()
{
    std::cout << "test 4" << std::endl;
    int ages[] = {0, 45, 89, 37, 72, 32, 42, 12, 52, 34, 22, 25, 26, 28, 19, 27, 24, 22, 99};
    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;

    sortAges(ages, 19);

    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_5()
{
    std::cout << "test 5" << std::endl;
    int ages[] = {0, 45, 89, 37, 72, 32, 42, 12, 52, 34, 22, 25, 26, 28, 19, 27, 100, 22, 42};
    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;

    sortAges(ages, 19);

    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_6()
{
    std::cout << "test 6" << std::endl;
    int ages[] = {0, 45, 89, 37, 72, 32, 42, 12, 52, 34, 22, 25, 26, 28, 19, 27, 100, 22, 42};
    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;

    sortAges(ages, 0);

    for(auto e:ages)
        std::cout << e << " ";
    std::cout << std::endl;
}

void test_sortAges()
{
    test_1();
    test_2();
    test_3();
    test_4();
    test_5();
    test_6();

}

int main(int argc, char**argv)
{

    test_sortAges();
    return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值