
Bucket Sort is a distribution-based sorting algorithm that works by dividing elements into several "buckets" and then sorting each bucket individually. It is particularly effective for sorting numbers that fall within a bounded range or when sorting elements based on their frequency. Bucket Sort shines when the input data has some form of structure or constraint that can be exploited.
It is particularly effective when:
In these scenarios, Bucket Sort reduces the problem from a global ordering task to a set of smaller, more manageable local sorts. When the data is reasonably well distributed, this leads to very efficient performance.
Bucket Sort belongs to the family of distribution-based sorting algorithms, along with Counting Sort and Radix Sort.
All three share the same core idea: instead of comparing elements directly, they group elements based on their value or position and then reconstruct the final ordering.
Because of this, Bucket Sort can be seen as a flexible middle ground: more general than Counting Sort, but less rigid than Radix Sort, making it a powerful tool when value distribution can be reasonably predicted.
function bucketSort(arr: number[], bucketCount: number = 5): number[] {
if (arr.length <= 1) return arr;
// Determine min and max values
const minValue = Math.min(...arr);
const maxValue = Math.max(...arr);
// Initialize buckets
const buckets: number[][] = Array.from({ length: bucketCount }, () => []);
// **** Scatter elements into buckets ****
// Rule to distribute CAN CHANGE based on the REQUIREMENTS of the problem.
// ----
for (const num of arr) {
const index = Math.floor(((num - minValue) / (maxValue - minValue + 1)) * bucketCount);
buckets[index].push(num);
}
// ----
// **** Sort individual buckets and concatenate ****
// Sort CAN CHANGE based on the REQUIREMENTS of the problem.
// ----
const sortedArray: number[] = [];
for (const bucket of buckets) {
bucket.sort((a, b) => a - b); // using built-in sort (can use insertion sort)
sortedArray.push(...bucket);
}
// ----
return sortedArray;
}
// Example usage
const numbers = [0.42, 0.32, 0.23, 0.52, 0.25, 0.47];
console.log(bucketSort(numbers, 5));
Bucket Sort is not a general-purpose sorting algorithm, but it can be extremely efficient when the input data has known and favorable characteristics.
A classic use case is sorting floating-point numbers in the range [0, 1). When values are uniformly distributed, each element can be mapped directly to a bucket using a simple
arithmetic formula. Because each bucket ends up containing only a small number of elements, the cost of sorting inside buckets is minimal, allowing Bucket Sort to run
in linear time in practice.
Another common application involves frequency-based problems, such as sorting characters or words by how often they appear. In these scenarios, buckets represent frequency values rather than numeric ranges. Elements are placed directly into the bucket corresponding to their count, and iterating over the buckets in order produces the desired result without relying on comparison-based sorting.
Bucket Sort is also useful in problems that require analyzing the distribution of values rather than fully sorting them. A notable example is finding the maximum gap between adjacent elements in sorted order. By storing only the minimum and maximum values in each bucket, it is possible to compute the result efficiently without performing a complete sort, achieving linear time complexity.
Bucket Sort’s efficiency strongly depends on how evenly elements are distributed across buckets:
| Problem | Technique | Solution |
|---|---|---|
| Sort Characters By Frequency | Bucket Sort | Solution |
| Top K Frequent Words | Bucket Sort | Solution |
| Maximum Gap | Bucket Sort | Solution |