Skip to main content

Bucket Sort

Definition​

Bucket Sort is a comparison sort algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm or by recursively applying the bucket sort algorithm

Practice​

bucketSort(arr):
n = length(arr)
create n empty buckets
max_val = maximum value in arr
min_val = minimum value in arr
range = (max_val - min_val) / n

// Scatter elements into buckets
for each element in arr:
bucket_index = (element - min_val) / range
append element to bucket[bucket_index]

// Sort each bucket
for each bucket in buckets:
sort(bucket)

// Concatenate sorted buckets
sorted_arr = []
for each bucket in buckets:
append bucket to sorted_arr

return sorted_arr