NON-COMPARISON SORTS
Bucket Sort, visualised
Bucket sort distributes elements into several buckets by value, sorts each bucket, and concatenates the buckets in order.
Run Bucket Sort in the visualizer →
Opens the Sorting Cinema visualizer with 16 elements in English.
How it works
- Choose the number of buckets (this app uses the larger of n ÷ 4 and 5).
- Put each element into a bucket in proportion to its value.
- Sort each bucket (this app uses JavaScript's built-in sort, and this step is not animated).
- Write the buckets back into the array from smallest to largest.
Complexity and stability
- Best
- — (no claim on this page)
- Average
- — (no claim on this page)
- Worst
- — (no claim on this page)
- Extra space
- O(n + number of buckets)
- Stability
- — (depends on how each bucket is sorted)
Complexity depends on the value distribution and the method used inside buckets. Evenly spread values keep buckets small, but if values pile into one bucket, sorting that bucket dominates. This page makes no specific Big-O claim.
Source of facts: Complexity is derived from the steps of this app's implementation (src/SortingCinema.jsx).
Trace on a small array
The first operations on the input [5, 2, 4, 1, 3], following the same steps as the app.
| # | Operation | Array after |
|---|---|---|
| 1 | Put [0]=5 into a bucket | [5, 2, 4, 1, 3] |
| 2 | Put [1]=2 into a bucket | [5, 2, 4, 1, 3] |
| 3 | Put [2]=4 into a bucket | [5, 2, 4, 1, 3] |
| 4 | Put [3]=1 into a bucket | [5, 2, 4, 1, 3] |
| 5 | Put [4]=3 into a bucket | [5, 2, 4, 1, 3] |
| 6 | Write 1 to [0] | [1, 2, 4, 1, 3] |
| 7 | Write 2 to [1] | [1, 2, 4, 1, 3] |
| 8 | Write 3 to [2] | [1, 2, 3, 1, 3] |
…and so on. In this trace, 10 compare / swap / write operations in total produce [1, 2, 3, 4, 5].
Suitable inputs and caveats
- Suited to values spread evenly across the range.
- Skewed inputs such as the “many duplicates” preset concentrate elements in a few buckets.
- This app animates distribution and write-back; sorting within buckets is done in one go with the built-in sort.
FAQ
How does bucket sort differ from counting sort?
Counting sort counts each individual value; bucket sort collects elements into containers covering value ranges and sorts each container with another method.
How are the buckets sorted?
This app sorts each bucket with JavaScript's built-in sort, and that step is not shown in the animation.