【桶排序的思想】
- 若待排序的值在一个有限范围内(整型)时,可设计有限个有序桶,桶号就是待排序的值。
- 之后,将待排序的值依次装入对应的有序桶中(重复值都装入对应的桶),然后顺序输出各桶的值,便得到有序的序列。
【C语言代码】
#include <stdio.h>
#include <math.h>
const int maxn=500;
int t[maxn];
int main() {
int n;
scanf("%d",&n);
int x; //x is less than maxn
int i,j;
for(i=1; i<=n; i++) {
scanf("%d",&x);
t[x]++;
}
for(j=1; j<=maxn; j++) {
while(t[j]>0) {
printf("%d ",j);
t[j]--;
}
}
return 0;
}
/*
in:
10
20 40 32 67 40 20 89 300 400 15
out:
15 20 20 32 40 40 67 89 300 400
*/