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),常量大小辅助空间