From b5bbcb046510e54d1235f7db32903cf2b39e5aa5 Mon Sep 17 00:00:00 2001 From: Martin Xu Date: Wed, 17 Apr 2024 22:46:36 +0800 Subject: [PATCH 1/4] feat: add ruby code block - bucket sort --- codes/ruby/chapter_sorting/bucket_sort.rb | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 codes/ruby/chapter_sorting/bucket_sort.rb diff --git a/codes/ruby/chapter_sorting/bucket_sort.rb b/codes/ruby/chapter_sorting/bucket_sort.rb new file mode 100644 index 0000000000..a51127d069 --- /dev/null +++ b/codes/ruby/chapter_sorting/bucket_sort.rb @@ -0,0 +1,45 @@ +=begin +File: bucket_sort.rb +Created Time: 2024-04-17 +Author: Martin Xu (martin.xus@gmail.com) +=end + +### 桶排序 ### +def bucket_sort(nums) + # 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素 + k = nums.length / 2 + buckets = [] + 0.upto(k - 1) do |i| + buckets << [] + end + + # 1. 将数组元素分配到各个桶中 + nums.each do |num| + # 输入数据范围为 [0, 1),使用 num * k 映射到索引范围 [0, k-1] + i = (num * k).to_i + # 将 num 添加进桶 i + buckets[i] << num + end + + # 2. 对各个桶执行排序 + buckets.each do |bucket| + # 使用内置排序函数,也可以替换成其他排序算法 + bucket.sort! + end + + # 3. 遍历桶合并结果 + i = 0 + buckets.each do |bucket| + bucket.each do |num| + nums[i] = num + i += 1 + end + end +end + +# 测试代码 +if __FILE__ == $0 + nums = [0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37] + bucket_sort(nums) + puts "桶排序完成后 nums = #{nums}" +end \ No newline at end of file From b6b9f5435ed758c63296b0432a3c762a5b145389 Mon Sep 17 00:00:00 2001 From: Martin Xu Date: Fri, 19 Apr 2024 08:28:37 +0800 Subject: [PATCH 2/4] Update codes/ruby/chapter_sorting/bucket_sort.rb Co-authored-by: khoaxuantu <68913255+khoaxuantu@users.noreply.github.com> --- codes/ruby/chapter_sorting/bucket_sort.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/codes/ruby/chapter_sorting/bucket_sort.rb b/codes/ruby/chapter_sorting/bucket_sort.rb index a51127d069..f761f3a44a 100644 --- a/codes/ruby/chapter_sorting/bucket_sort.rb +++ b/codes/ruby/chapter_sorting/bucket_sort.rb @@ -8,10 +8,7 @@ def bucket_sort(nums) # 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素 k = nums.length / 2 - buckets = [] - 0.upto(k - 1) do |i| - buckets << [] - end + buckets = Array.new(k) { [] } # 1. 将数组元素分配到各个桶中 nums.each do |num| From ec57d79114437927c8c2201fc48e9ff75fe9441c Mon Sep 17 00:00:00 2001 From: Martin Xu Date: Fri, 19 Apr 2024 08:28:46 +0800 Subject: [PATCH 3/4] Update codes/ruby/chapter_sorting/bucket_sort.rb Co-authored-by: khoaxuantu <68913255+khoaxuantu@users.noreply.github.com> --- codes/ruby/chapter_sorting/bucket_sort.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/codes/ruby/chapter_sorting/bucket_sort.rb b/codes/ruby/chapter_sorting/bucket_sort.rb index f761f3a44a..a745437686 100644 --- a/codes/ruby/chapter_sorting/bucket_sort.rb +++ b/codes/ruby/chapter_sorting/bucket_sort.rb @@ -36,6 +36,7 @@ def bucket_sort(nums) # 测试代码 if __FILE__ == $0 + # 设输入数据为浮点数,范围为 [0, 1) nums = [0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37] bucket_sort(nums) puts "桶排序完成后 nums = #{nums}" From af99bf0f4bbb16136b21b4295f67ca6e63c55ccc Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Tue, 30 Apr 2024 15:58:11 +0800 Subject: [PATCH 4/4] Update bucket_sort.rb --- codes/ruby/chapter_sorting/bucket_sort.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/ruby/chapter_sorting/bucket_sort.rb b/codes/ruby/chapter_sorting/bucket_sort.rb index a745437686..bcdbe81ae1 100644 --- a/codes/ruby/chapter_sorting/bucket_sort.rb +++ b/codes/ruby/chapter_sorting/bucket_sort.rb @@ -34,10 +34,10 @@ def bucket_sort(nums) end end -# 测试代码 +### Driver Code ### if __FILE__ == $0 # 设输入数据为浮点数,范围为 [0, 1) nums = [0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37] bucket_sort(nums) puts "桶排序完成后 nums = #{nums}" -end \ No newline at end of file +end