FlowLab Logo

Bucket Sort Visualizer

SlowFast
515
(10)
Ready to start sorting

Array Elements

76
[0]
75
[1]
10
[2]
21
[3]
89
[4]
91
[5]
71
[6]
82
[7]
42
[8]
91
[9]
Active
Sorted
Default
Operation Logs
No operations yet
Algorithm Info
Time Complexity: O(n + k) for n bars and k buckets
Space Complexity: O(n + k)
Stable: Yes, if using stable sort inside buckets
How it works: Divides elements into buckets, sorts each bucket individually, then concatenates all buckets.
Best for: Uniformly distributed data, floating points
Implementation
function bucketSort(arr) {
  const n = arr.length;
  const max = Math.max(...arr);
  const min = Math.min(...arr);
  const bucketCount = Math.max(5, Math.floor(n / 2));
  const buckets = Array.from({length: bucketCount}, () => []);
  for (let i = 0; i < n; i++) {
    const idx = Math.floor(((arr[i] - min) / (max - min + 1e-8)) * bucketCount);
    buckets[Math.min(idx, bucketCount-1)].push(arr[i]);
  }
  for (let b = 0; b < bucketCount; b++) {
    buckets[b].sort((a, b) => a - b);
  }
  return [].concat(...buckets);
}