首页 > 学院 > 开发设计 > 正文

实现一个算法,对公司员工年龄进行排序,时间效率要求O(n)

2019-11-06 07:40:04
字体:
来源:转载
供稿:网友
//实现一个算法,对公司员工年龄进行排序,时间效率要求O(n)#include<iostream>void sortAges(int ages[], int length){ if (ages == NULL || length < 0) { return ; } //因为年龄是一个小范围的内,我们可以使用辅助内存 const int oldestAge = 99; int timesOfAge[oldestAge + 1] = {0}; for (int i = 0; i < length; ++i) { int age = ages[i]; if (age<0 || age>oldestAge) { throw new std::exception("age out_of range."); } ++timesOfAge[age]; } int index = 0; for (int i = 0; i <= oldestAge; ++i) { for (int j = 0; j < timesOfAge[i]; ++j) { ages[index] = i; ++index; } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表