import java.util.ArrayList;
import java.util.Collections;
public class BucketSort {
public static void bucketSort(int[] array, int bucketSize) {
if (array.length <= 1) {
return;
}
// 找到最大值和最小值
int minValue = array[0];
int maxValue = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < minValue) {
minValue = array[i];
} else if (array[i] > maxValue) {
maxValue = array[i];
}
}
// 计算桶的数量
int bucketCount = (maxValue - minValue) / bucketSize + 1;
ArrayList<ArrayList<Integer>> buckets = new ArrayList<>(bucketCount);
for (int i = 0; i < bucketCount; i++) {
buckets.add(new ArrayList<>());
}
// 将元素分配到桶中
for (int i = 0; i < array.length; i++) {
int bucketIndex = (array[i] - minValue) / bucketSize;
buckets.get(bucketIndex).add(array[i]);
}
// 对每个桶内的元素进行排序
int currentIndex = 0;
for (int i = 0; i < bucketCount; i++) {
ArrayList<Integer> bucket = buckets.get(i);
Collections.sort(bucket);
for (int j = 0; j < bucket.size(); j++) {
array[currentIndex++] = bucket.get(j);
}
}
}
public static void main(String[] args) {
int[] array = {29, 25, 3, 49, 9, 37, 21, 43};
int bucketSize = 10;
System.out.println("原始数组:");
printArray(array);
bucketSort(array, bucketSize);
System.out.println("排序后数组:");
printArray(array);
}
private static void printArray(int[] array) {
for (int i : array) {
System.out.print(i + " ");
}
System.out.println();
}
}