3.6.8 · D4Sorting & Searching

Exercises — Bucket sort — uniform distributions

2,062 words9 min readBack to topic

Notation reminder (all earned in the parent note):

  • = number of input values.
  • Values unless a range is stated.
  • Bucket index of a value : — the floor (round-down) of times . "Floor" = largest integer not exceeding ; e.g. .
  • = how many values land in bucket .

Level 1 — Recognition

L1·1 — Which bucket?

You have buckets (indices ) and value . Which bucket index does it get?

Recall Solution

WHAT: apply the index formula . WHY: stretching by turns the value into an address; the floor picks the slot. Answer: bucket 2. (Values in all map here — check: , .)

L1·2 — Spot the assumption

Which single input assumption turns bucket sort's worst case into an expected ?

Recall Solution

A uniform distribution over a known range. It guarantees each element is equally likely to land in any bucket, so buckets fill evenly — about one item each. See Binomial distribution for why "equally likely per bucket" gives .

L1·3 — Why not comparison-bound?

True or false: bucket sort violates the comparison-sort lower bound.

Recall Solution

False — it does not violate it; the bound simply does not apply. The Comparison sort lower bound only constrains algorithms that decide order by comparing pairs. Bucket sort's scatter step uses the value as an address (like Hashing), not a comparison, so it is outside the theorem's scope.


Level 2 — Application

L2·1 — Scatter a small array

Bucket-sort by hand: , input [0.10, 0.60, 0.30, 0.90]. Show buckets, then the sorted output.

Recall Solution

Step — scatter with index :

Buckets: [0]=[0.10], [1]=[0.30], [2]=[0.60], [3]=[0.90]. Step — sort each: each has one element → nothing to do. Step — concatenate in order: [0.10, 0.30, 0.60, 0.90] ✅. A textbook "one item per bucket" case — see the histogram in the figure below.

Figure — Bucket sort — uniform distributions

L2·2 — Range remapping

Values live in . With buckets, which bucket holds ?

Recall Solution

WHY normalize first: the index formula assumes . We rescale the value into using , then bucket. Answer: bucket 3.

L2·3 — Count the per-bucket load

Input [0.12, 0.17, 0.21, 0.23, 0.26], . Which buckets are non-empty and how many in each?

Recall Solution

Index :

Bucket 1 holds 2 items; bucket 2 holds 3 items; all others empty. Largest load , so the costliest per-bucket Insertion sort handles only 3 elements — cheap.


Level 3 — Analysis

L3·1 — Compute for

For elements and buckets, each . Find .

Recall Solution

WHAT tool: the identity , because we know mean and variance of a Binomial but not directly. With : This matches the general formula . Constant, below 2 — the reason total work stays linear.

L3·2 — Total expected quadratic work

Using L3·1, what is the expected value of ?

Recall Solution

Linearity of expectation: . Compare to the "perfect" one-per-bucket world where . The extra is the clumping penalty from variance — still , not .

L3·3 — Worst-case construction

Construct an input in that forces all elements into one bucket, and state the resulting time.

Recall Solution

Put every value inside a single slot's range. Bucket owns . Choose [0.01, 0.05, 0.10, 0.20]: All 4 land in bucket 0. That bucket's Insertion sort costs . Worst case — the histogram (right panel below) shows the single tall spike vs. the flat uniform case.

Figure — Bucket sort — uniform distributions

Level 4 — Synthesis

L4·1 — Choosing bucket count

You will sort uniform values. A friend suggests buckets "to avoid all collisions". Give the expected total time for buckets and explain the sweet spot.

Recall Solution

With buckets, creation + concatenation cost ; scatter costs ; each so expected sort work is . Total: .

  • : the term dominates wasted work on empty buckets.
  • : , everything is . ✅

Sweet spot — one bucket per expected element.

L4·2 — Stability by design

Modify bucket sort so it is stable (equal keys keep their original relative order). Explain each choice.

Recall Solution

Two requirements:

  1. Scatter in original order and append — so within a bucket, earlier elements sit earlier. (Insertion sort preserves this if it's a stable sort.)
  2. Per-bucket sort must be stable. Insertion sort is stable, so use it (not an unstable quicksort).

Because concatenation reads buckets left-to-right and each bucket kept insertion order among equal keys, the whole output is stable. This is exactly why Counting sort (bucket sort with size-1 integer buckets) is famously stable.

L4·3 — Bucket sort as a radix pass

Explain how one pass of bucket sort on the leading base-10 digit relates to Radix sort, and what breaks if you stop after one pass.

Recall Solution

Bucketing on the leading digit is MSD (most-significant-digit) radix sort's first pass: it groups by high-order digit. Within a bucket, elements are not yet fully ordered — only their leading digit agrees. What breaks after one pass: two values 0.412 and 0.418 share bucket 4 but stay unsorted relative to each other. Radix sort fixes this by recursively bucketing each bucket on the next digit (or LSD radix does it back-to-front with a stable pass). Bucket sort's own fix is simpler: sort each bucket directly with insertion sort.


Level 5 — Mastery

L5·1 — Expected max bucket load (intuition + estimate)

For uniform values into buckets, roughly how large is the largest bucket ? Give the order and the intuition (no exact proof required).

Recall Solution

WHAT tool: the balls-into-bins result, driven by the Binomial distribution tail. Each for large . Estimate: with high probability. Intuition: with nearly-independent Poisson(1) buckets, the tail probability ; the largest of such draws is around the where , i.e. , giving . Why it matters: even the worst bucket is only poly-logarithmic, so no single insertion sort blows up the linear expectation — the average is robust, not luck.

L5·2 — Break the uniform assumption gracefully

Inputs are drawn from a known but non-uniform density on (e.g. skewed toward 0). Design a bucketing rule that still gives one element per bucket, and justify it.

Recall Solution

Idea: make buckets equal-probability, not equal-width. Use the cumulative distribution , which maps to "fraction of mass below ", so is uniform on by construction. Rule: bucket index . Why it works: because is uniform for any continuous (the "probability integral transform"), each bucket now receives probability again → the same expected even on skewed data. Cost of the fix: you must know (or estimate) . If you don't, skewed data reverts to the worst case.

L5·3 — Full trace under adversarial reordering

Take , input [0.99, 0.02, 0.51, 0.50, 0.98, 0.03]. Trace the scatter, per-bucket sort, and final output. Confirm order and stability.

Recall Solution

Index :

Buckets (append order preserved):

  • [0] = [0.02, 0.03]
  • [3] = [0.51, 0.50]
  • [5] = [0.99, 0.98]

Sort each: [0]=[0.02,0.03], [3]=[0.50,0.51], [5]=[0.98,0.99]. Concatenate: [0.02, 0.03, 0.50, 0.51, 0.98, 0.99] ✅ fully sorted. Stability check: no duplicate keys here, but note and shared bucket 3 and were reordered by value (correct) — stability only concerns equal keys, which are untouched.


Recall checkpoint


Connections