原理:提前确定好要排序的最大数。 然后排序。
public static void sort(int[] ints, Boolean isAsc) {
int[] basket = new int[101]; //定义足够大的数组桶
for (int i : ints) {
basket[i]++; //对应元素的桶下标自增
}
if (isAsc) {
for (int i = 0; i < basket.length - 1; i++) {
if (basket[i] > 0) {
for (int j = 1; j <= basket[i]; j++) {
System.out.print(i + " "); //循环输出桶元素不为0的下标值
}
}
}
} else {
for (int i = basket.length - 1; i > 0; i--) {
if (basket[i] > 0) {
for (int j = 1; j <= basket[i]; j++) {
System.out.print(i + " "); //循环输出桶元素不为0的下标值
}
}
}
}
}
public static void main(String[] args) {
int[] ints = {2, 6, 4, 9, 12, 98, 5, 32, 90, 33, 24, 65, 37, 12, 4};
sort(ints, true);
}