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.

The CDF: how to choose bucket boundaries

The cumulative distribution function (CDF) of a random variable X gives the probability that X does not exceed a threshold x. It is nondecreasing, lies between 0 and 1, and approaches 0 or 1 at the ends of the number line.

FX(x)=P(X≤x)

If F is continuous and known, F(X) is uniform between 0 and 1. For k buckets numbered 0 to k−1, use b(x) = min(k−1, ⌊k F(x)⌋). Bucket boundaries are the quantiles F⁻¹(i/k): each interval has model probability 1/k. The minimum handles F(x)=1.

CDF from a density

For a continuous variable with density f, integrate: F(x) = ∫₋∞ˣ f(t) dt. If X is uniform from a to b, within that interval F(x) = (x−a)/(b−a). Outside [a,b] it is 0 to the left and 1 to the right. For example, on [0, 100], F(35)=0.35: with 10 buckets, 35 goes to bucket 3.

CDF from discrete probabilities

Sum the probabilities of values no greater than x: F(x) is the sum of P(X=v) over all v≤x. If P(X=1)=0.2, P(X=2)=0.5 and P(X=3)=0.3, then F(2)=0.7. The CDF has jumps: applying F(X) directly does not balance buckets when many values are equal.

CDF estimated from data

From a sample of m observations, F̂ₘ(x) = #{values ≤ x}/m. For 1, 2, 2, 5, 8 we get F̂₅(2)=3/5. A representative sample can suggest quantiles, but estimation has a cost and errors can leave buckets unbalanced.

Count the cost: Expected Θ(n+k) time requires buckets with small expected occupancy and a CDF evaluable in O(1) per item (otherwise add its evaluation cost). The standard visualizer below uses equal value intervals; the circular lab instead shows CDF based buckets.

Circular example: sectors and rings

Following the example in Algoritmi.pdf, consider points uniformly distributed over the area of a disk of radius R. If the key is angle θ, sectors have width 2π/k and FΘ(θ)=θ/(2π). If the key is distance r from the centre, equal width rings are not balanced: area grows with r². Use equal area rings instead.

Sort by angle

0 ≤ θ < 2π · FΘ(θ) = θ/(2π) · b(θ) = ⌊kθ/(2π)⌋

With k=8, a point at θ=π/2 goes to bucket 2.

Sort by radius

0 ≤ r ≤ R · Fᵣ(r) = πr²/(πR²) = (r/R)² · rᵢ = R√(i/k)

With R=10 and k=4, the first ring ends at r₁=5; each ring covers 25% of the area.

Sort key

Select a point in the disk, or advance with the controls.

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.