时间复杂度为O(n)的排序

题目:输入一组员工的年龄,是o(n)的时间复杂度将他们排序

思路:

1.采用以空间效率换时间效率的思想,新建一个长度为100的数组countage来保存0-100岁员工年龄出现的次数

2.遍历countage将年龄赋值ages数组,年龄出现几次就赋值几次,例如:countage[18]=3时,代表18岁的员工有3人,则将18给ages赋值3次。

#include<iostream>
using namespace std;
void sortages(int ages[],int length)
{
    if(ages==NULL||length<=0)
        throw exception("参数有误!");
    const int large=99;
    int countage[large+1];//定义保存年龄次数的数组
    //初始化countage
    for(int i=0;i<=large;i++)
        countage[i]=0;
    //为countage赋初值
    for(int i=0;i<length;i++)
    {
        if(ages[i]<0||ages[i]>99)
            throw exception("年龄数组输入有误");
        ++countage[ages[i]];//这里ages[i]就相当于hash函数,即它ages数组本身就是countage数组的地址
    }
    int index=0;//ages的下标
    //ages保存经过排序后的年龄
    for(int i=18;i<60;i++)//18-60岁的年龄
    {
        for(int j=0;j<countage[i];j++)//有几个就赋值几次
        {
            ages[index]=i;
            index++;
        }
    }
}
//打印方法
void print(int ages[],int length)
{
    for(int i=0;i<length;i++)
    {
        cout<<" "<<ages[i];
    }
}
int main()
{
    int ages[10]={18,29,34,19,54,24,32,35,55,47};
    sortages(ages,10);
    print(ages,10);
    return 0;
}

测试结果:

原文地址:https://www.cnblogs.com/runninglzw/p/4488498.html