Sorting algorithms · 7/7

Bucket sort

Distributes by range, sorts locally and concatenates.

Average timeΘ(n + k) SpaceΘ(n + k) StableDepends In placeNo

How it works

Each bucket covers an ordered interval. Sort inside each bucket and concatenate left to right.

Invariant: Every value in bucket i precedes values in later buckets; only local order remains.

Step-by-step visualization

Each bar represents one element. Colors identify compared elements, the pivot or key, the final region and the active range.

Use 2 to 16 values. This visualizer accepts values from 0 to 999.

Comparisons
0
Writes
0
Swaps
0
Pass / level
0
active comparison pivot / key final

Pseudocode linked to the visualization

  1. create k empty buckets
  2. for value in A
  3. index ← bucketFor(value)
  4. append value to bucket[index]
  5. for each bucket
  6. sort bucket
  7. concatenate all buckets into A

The highlighted line corresponds to the operation described by the visualizer. Index-management details are intentionally simplified.

Complexity analysis

Best caseΘ(n + k)

Well-balanced buckets make distribution and collection Θ(n+k).

Average caseΘ(n + k)

Under a uniform model with k=Θ(n), expected bucket size is constant: Θ(n+k).

Worst caseΘ(n²)

If every value lands in one insertion-sorted bucket, time is Θ(n²).

Auxiliary spaceΘ(n + k)

The k buckets hold n total items: Θ(n+k). Stability depends on the local sort.

Properties at a glance

Conditional stability

Stability concerns the relative order of elements with equal keys.

Not in place

This classification refers to the version shown on this page.

When to use it and what to avoid

A good choice when…

Effective when a roughly uniform distribution is known.

Common mistake

Bucket boundaries are part of the algorithm; poor boundaries cause imbalance.