桶排序
剑指offer 2.4.1
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void sortAges(int a[],int n) 5 { 6 const int MAXAGE=99; 7 if(a==NULL||n<=0) 8 return; 9 int ageTimes[MAXAGE+1]; 10 int i,j,age,counts; 11 for(i=0; i<=MAXAGE; ++i) 12 ageTimes[i]=0; 13 //读入年龄放入数组中 14 for(i=0; i<n; ++i) 15 { 16 age=a[i]; 17 ageTimes[age]++; 18 } 19 //把排完序的结果写入数组中 20 int k=0; 21 for(i=0; i<=MAXAGE; ++i) 22 { 23 counts=ageTimes[i]; 24 for(j=0; j<counts; ++j) 25 { 26 a[k]=i; 27 k++; 28 } 29 30 } 31 32 } 33 34 int main() 35 { 36 int a[]= {10,10,3,4,5,6,6,72,2,4,43,2,2,1}; 37 int n=sizeof(a)/sizeof(int); 38 int i; 39 sortAges(a,n); 40 for(i=0; i<n; ++i) 41 printf("%d\n",a[i]); 42 return 0; 43 }
posted on 2014-08-23 17:07 crane_practice 阅读(115) 评论(0) 编辑 收藏 举报