O(N)排序公司员工年龄


void sortages(int ages[], int length)
{
	if ((ages == NULL) || length <= 0)
		return;

	int oldage = 99;
	int timeofage[100] = {0};//用来记录1-99岁每个年龄的人数。
	for (int i = 0; i < length; i++)
	{
		int age = ages[i];
		if (age < 0 || age > 99)
		{
			return;
		}
		timeofage[age]++;
	}
	int index = 0;
	for (int i = 1; i <= oldage; ++i)
	{
		for (int j = 0; j < timeofage[i]; ++j)//控制某个年龄人数的次数
		{
			ages[index] = i;
			++index;
		}
	}
}

你可能感兴趣的:(剑指offer)