diff --git a/codes/csharp/chapter_sorting/bucket_sort.cs b/codes/csharp/chapter_sorting/bucket_sort.cs index ab02924df..6b6d0de48 100644 --- a/codes/csharp/chapter_sorting/bucket_sort.cs +++ b/codes/csharp/chapter_sorting/bucket_sort.cs @@ -18,7 +18,7 @@ public class bucket_sort { // 1. 将数组元素分配到各个桶中 foreach (float num in nums) { // 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1] - int i = (int)num * k; + int i = (int) (num * k); // 将 num 添加进桶 i buckets[i].Add(num); } diff --git a/codes/go/chapter_sorting/bucket_sort.go b/codes/go/chapter_sorting/bucket_sort.go index 32ce56fb4..af4206fe0 100644 --- a/codes/go/chapter_sorting/bucket_sort.go +++ b/codes/go/chapter_sorting/bucket_sort.go @@ -17,7 +17,7 @@ func bucketSort(nums []float64) { // 1. 将数组元素分配到各个桶中 for _, num := range nums { // 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1] - i := int(num) * k + i := int(num * float64(k)) // 将 num 添加进桶 i buckets[i] = append(buckets[i], num) } diff --git a/codes/java/chapter_sorting/bucket_sort.java b/codes/java/chapter_sorting/bucket_sort.java index cc3f5938e..e47ed40f1 100644 --- a/codes/java/chapter_sorting/bucket_sort.java +++ b/codes/java/chapter_sorting/bucket_sort.java @@ -20,7 +20,7 @@ public class bucket_sort { // 1. 将数组元素分配到各个桶中 for (float num : nums) { // 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1] - int i = (int) num * k; + int i = (int) (num * k); // 将 num 添加进桶 i buckets.get(i).add(num); } diff --git a/codes/swift/chapter_sorting/bucket_sort.swift b/codes/swift/chapter_sorting/bucket_sort.swift index 1967695c9..5da508414 100644 --- a/codes/swift/chapter_sorting/bucket_sort.swift +++ b/codes/swift/chapter_sorting/bucket_sort.swift @@ -12,7 +12,7 @@ func bucketSort(nums: inout [Double]) { // 1. 将数组元素分配到各个桶中 for num in nums { // 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1] - let i = Int(num) * k + let i = Int(num * k) // 将 num 添加进桶 i buckets[i].append(num) }