Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions algorithms/sorting/bucket_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# A Ruby implementation of the Bucket Sort algorithm

require "pp"

def bucket_sort(unsorted_array)
# Display unsorted array
puts "Unsorted array"
pp unsorted_array
puts "\n"

# Create empty buckets
buckets = bead_grid = Array.new(unsorted_array.length) { Array.new }

# Put array elements in buckets
unsorted_array.each_with_index do |value, index|
bucket_index = unsorted_array.size * value
buckets[bucket_index].append(value)
end

# Display buckets
puts "Buckets to be sorted"
pp buckets
puts "\n"

# Sort individual buckets
buckets.each do |value|
value.sort!
end

# Flatten buckets into one array
sorted_array = buckets.flatten

# Display sorted array
puts "Sorted array"
pp sorted_array
puts "\n"

# Return sorted_array
sorted_array
end

# Testing the algorithm
array = [0.65, 0.22, 0.05, 0.17]
bucket_sort(array)

# Output
#
# Unsorted array
# [0.65, 0.22, 0.05, 0.17]
#
# Buckets to be sorted
# [[0.22, 0.05, 0.17], [], [0.65], []]
#
# Sorted array
# [0.05, 0.17, 0.22, 0.65]
#