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

//实现一个算法,对公司员工年龄进行排序,时间效率要求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; } } }