Skip to content

Bucket Sort

Bucket sort divides elements into buckets (ranges), sorts each bucket individually (usually with insertion sort), then concatenates the buckets.


flowchart LR
A["Input: [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68]"] --> B["Buckets<br/>(10 buckets for 0.0-1.0)"]
subgraph Buckets[Buckets]
B0["[0.12, 0.17]"]
B1["[0.21, 0.23, 0.26]"]
B2["[0.39]"]
B3["[0.68]"]
B4["[0.72, 0.78]"]
B5["[0.94]"]
end
B0 --> C["Sort each bucket<br/>(insertion sort)"]
B1 --> C
B2 --> C
B3 --> C
B4 --> C
B5 --> C
C --> D["Concatenate all buckets"]
D --> E["✅ [0.12, 0.17, 0.21, 0.23, 0.26, 0.39, 0.68, 0.72, 0.78, 0.94]"]
style A fill:#7c3aed,color:#fff
style B0 fill:#4f46e5,color:#fff
style B1 fill:#6366f1,color:#fff
style B2 fill:#4f46e5,color:#fff
style B3 fill:#6366f1,color:#fff
style B4 fill:#4f46e5,color:#fff
style B5 fill:#6366f1,color:#fff
style E fill:#059669,color:#fff
function bucketSort(arr, bucketSize = 5) {
if (arr.length === 0) return arr;
const min = Math.min(...arr);
const max = Math.max(...arr);
const bucketCount = Math.floor((max - min) / bucketSize) + 1;
const buckets = Array.from({ length: bucketCount }, () => []);
// Scatter into buckets
for (const num of arr) {
const idx = Math.floor((num - min) / bucketSize);
buckets[idx].push(num);
}
// Sort each bucket and gather
const result = [];
for (const bucket of buckets) {
insertionSort(bucket); // small bucket → insertion is fast
result.push(...bucket);
}
return result;
}
function insertionSort(arr) {
for (let i = 1; i < arr.length; i++) {
const key = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

ConditionTime
Uniform distributionO(N + K) — near linear
Worst case (all in one bucket)O(N²)
SpaceO(N + K)

Key insight: Bucket sort is fastest when data is uniformly distributed. If data clusters, many items land in the same bucket and sorting that bucket is O(N²).


UseDon’t Use
Uniformly distributed floating point dataHeavily clustered data
When you need near-linear time on averageWhen worst-case matters
Sorting test scores, gradesSmall arrays (N < 50)

SortTypeTimeBest For
CountingFrequency countingO(N + K)Small integer range
RadixDigit-by-digitO(d × N)Fixed-width integers
BucketDistribution + insertionO(N + K) avgUniformly distributed floats

  • Bucket sort spreads items into ranges (buckets), sorts each one cheaply, then combines.
  • Works great when data is evenly spread across the range.
  • Use with floating point numbers that are uniformly distributed.