Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bucket Sort algorithm #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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]
#