桶排序步骤:
1.将值域分成若干段,每段置于桶中
2.将待排序元素放入对应桶中
3.将个桶内的元素进行排序
4.将桶中的元素依次取出
列如:
1 3 5 4 1 6 2 1 3 9 8 7
1 1 1 | 2 | 3 3 | 4 | 5 | 6 | 7 | 8 | 9 |
1 2 3 4 5 6 7 8 9 10
从桶中取出依次排序
1,1,1,2,3,3,4,5,6,7,8,9
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int[] a=new int[(int) 5e5];
int n=scan.nextInt();
for (int i = 0; i < n; i++) {
int s=scan.nextInt();
a[s]++;
}
for (int i = 0; i <=n; i++) {
for (int i1 = 1; i1 <= a[i]; i1++) {
System.out.print(i+" ");
}
}
}
}
输入:5
4 3 2 1 5
输出结果:1 2 3 4 5