#include <iostream>
#include<cstdlib>
using namespace std;
void sortAges(int ages[],int length){
if(ages==NULL||length<=0){
return;
}
const int oldestAge=99;
int timesofAge[oldestAge+1];
for(int i=0;i<=oldestAge;i++){
timesofAge[i]=0;
}
for(int i=0;i<length;++i){
int age=ages[i];
if(age<0||age>oldestAge)
throw "Invalid Input";
timesofAge[age]++;
}
int index=0;
for(int i=0;i<=oldestAge;i++){
for(int j=0;j<timesofAge[i];j++){
ages[index]=i;
index++;
}
}
}
int main() {
cout << "test\n";
int data[200];
for(int i=0;i<200;i++){
data[i]=rand()%100;
cout << data[i]<<'\t';
}
sortAges(data,200);
cout << endl;
for(int i=0;i<200;i++){
//data[i]=rand()%100;
cout << data[i]<<'\t';
}
//int num=rand()%100;
//cout << num;
return 0;
}
测试结果如下:
test
83 86 77 15 93 35 86 92 49 21 62 27 90 59 63 26 40 26 72 36 11 68 67 29 82 30 62 23 67 35 29 2 22 58 69 67 93 56 11 42 29 73 21 19 84 37 98 24 15 70 13 26 91 80 56 73 62 70 96 81 5 25 84 27 36 5 46 29 13 57 24 95 82 45 14 67 34 64 43 50 87 8 76 78 88 84 3 51 54 99 32 60 76 68 39 12 26 86 94 39 95 70 34 78 67 1 97 2 17 92 52 56 1 80 86 41 65 89 44 19 40 29 31 17 97 71 81 75 9 27 67 56 97 53 86 65 6 83 19 24 28 71 32 29 3 19 70 68 8 15 40 49 96 23 18 45 46 51 21 55 79 88 64 28 41 50 93 0 34 64 24 14 87 56 43 91 27 65 59 36 32 51 37 28 75 7 74 21 58 95 29 37 35 93 18 28 43 11 28 29
0 1 1 2 2 3 3 5 5 6 7 8 8 9 11 11 11 12 13 13 14 14 15 15 15 17 17 18 18 19 19 19 19 21 21 21 21 22 23 23 24 24 24 24 25 26 26 26 26 27 27 27 27 28 28 28 28 28 29 29 29 29 29 29 29 29 30 31 32 32 32 34 34 34 35 35 35 36 36 36 37 37 37 39 39 40 40 40 41 41 42 43 43 43 44 45 45 46 46 49 49 50 50 51 51 51 52 53 54 55 56 56 56 56 56 57 58 58 59 59 60 62 62 62 63 64 64 64 65 65 65 67 67 67 67 67 67 68 68 68 69 70 70 70 70 71 71 72 73 73 74 75 75 76 76 77 78 78 79 80 80 81 81 82 82 83 83 84 84 84 86 86 86 86 86 87 87 88 88 89 90 91 91 92 92 93 93 93 93 94 95 95 95 96 96 97 97 97 98 99