员工年龄排序

void AgeSort(int arrAges[], int nLength)
{
	const int nAgeMax = 99;
	int arrTemp[nAgeMax+1];
	for (int i = 0; i < nAgeMax; ++i)
	{
		arrTemp[i] = 0;
	}

	for (int i = 0; i < nLength; ++i)
	{
		int age = arrAges[i];
		if (age < 0 || age > nAgeMax)
			throw new std::exception("age out of range!");
		arrTemp[age]++;
	}

	int nIndex = 0;
	for (int i = 0; i < nAgeMax; ++i)
	{
		for (int j = 0; j < arrTemp[i]; ++j)
		{
			arrAges[nIndex] = arrTemp[j];
			++nIndex;
		}
	}
}
//要求:时间复杂度O(n),常量大小辅助空间
posted on 2021-02-07 15:03  Noora&w  阅读(76)  评论(0编辑  收藏  举报