parent
22898b6955
commit
c338a6d8d8
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* File: bucket_sort.java
|
||||
* Created Time: 2023-03-17
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class bucket_sort {
|
||||
/* 桶排序 */
|
||||
static void bucketSort(float[] nums) {
|
||||
// 初始化 k = n/3 个桶,预期向每个桶分配 3 个元素
|
||||
int k = nums.length / 2;
|
||||
List<List<Float>> buckets = new ArrayList<>();
|
||||
for (int i = 0; i < k; i++) {
|
||||
buckets.add(new ArrayList<>());
|
||||
}
|
||||
// 1. 将数组元素分配到各个桶中
|
||||
for (float num : nums) {
|
||||
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
|
||||
int i = (int) num * k;
|
||||
// 将 num 添加进桶 i
|
||||
buckets.get(i).add(num);
|
||||
}
|
||||
// 2. 对各个桶执行排序
|
||||
for (List<Float> bucket : buckets) {
|
||||
// 使用内置排序函数,也可以替换成其它排序算法
|
||||
Collections.sort(bucket);
|
||||
}
|
||||
// 3. 遍历桶合并结果
|
||||
int i = 0;
|
||||
for (List<Float> bucket : buckets) {
|
||||
for (float num : bucket) {
|
||||
nums[i++] = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 设输入数据为浮点数,范围为 [0, 1)
|
||||
float[] nums = { 0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.58f, 0.15f, 0.37f, 0.55f, 0.28f };
|
||||
bucketSort(nums);
|
||||
System.out.println("桶排序完成后 nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
After Width: | Height: | Size: 132 KiB |
After Width: | Height: | Size: 59 KiB |
After Width: | Height: | Size: 104 KiB |
Loading…
Reference in new issue