3.6.8 · D1Sorting & Searching

Foundations — Bucket sort — uniform distributions

1,764 words8 min readBack to topic

This page assumes nothing. If the parent note used a symbol, we build it here, from the picture up, in an order where each idea leans only on the ones before it.


1. A "list" (array) and its length

Picture a row of boxes side by side. If there are ten boxes, . That single number will appear everywhere: number of items, number of buckets, and the stretch factor when we compute an address.

Why the topic needs it. Bucket sort creates exactly buckets (one per expected element). So is doing double duty — it counts the input and sizes the workspace.

Figure — Bucket sort — uniform distributions

2. The half-open interval

Picture a ruler from 0 to 1. The left tip is a filled dot (included), the right tip is a hollow dot (excluded).

Why the topic needs it. Uniform inputs are stated to live in precisely so the address formula never overflows.

Figure — Bucket sort — uniform distributions

3. The floor function

Picture a staircase where each step is a whole number. Wherever stands on the ramp, floor drops it straight down to the step beneath its feet.

The two bent bars (like brackets missing their tops) are the floor symbol. In code this is int() for non-negative numbers.

Why the topic needs it. A bucket index must be a whole number (). The value is usually a decimal (e.g. ). Floor converts "somewhere in the range" into "which whole box" — it is the tool that turns a continuous value into a discrete address.


4. Subscripts: and

Picture each box in the row wearing a numbered sticker on its front. points at the value inside; counts how many values sit in bucket number .

Why the topic needs it. The proof sums over all buckets: . Without a name for "bucket 's load," we could not write that sum.


5. Putting it together: the address

Now every piece from §1–§4 combines into the single most important line of the whole topic.

WHAT this does, step by step (see figure):

  • Stretch: multiply by . This blows the tiny ruler up to the wide ruler .
  • Drop: floor the result. Every value in now maps to one of the whole numbers .

WHY multiply then floor and not something else? We want equal-width value slots to map to consecutive buckets. Multiplying by makes each slot of width become width ; flooring then reads off which unit-slot you're in. No comparisons, no searching — pure arithmetic. That is the "value-as-address" trick.

Figure — Bucket sort — uniform distributions

6. "Uniform distribution" — the assumption that makes it fast

Picture rain falling evenly across a flat roof: every tile catches about the same number of drops. Contrast with rain blown into one corner — that corner floods (a skewed distribution).

Why the topic needs it. Even rain → even bucket loads → each → each bucket trivially cheap to sort. This is the entire reason bucket sort beats . Break the assumption (all drops in one corner) and one bucket holds all items → the worst case.

Formally, "each item independently lands in bucket with probability " is the mathematical shape of "uniform." That statement is exactly what feeds the Binomial distribution used in the proof.

Figure — Bucket sort — uniform distributions

7. Big-O: , ,

Picture three curves climbing from the origin: a straight line (), a gently-bending sweep (), and a steep parabola (). Bucket sort aims for the straight line and falls to the parabola when the distribution assumption dies.

Why the topic needs it. The whole payoff is a claim about scaling: "expected ." You cannot state, prove, or appreciate that without this notation.


8. The sum symbol and expectation

Picture the sum as a conveyor belt tipping every bucket's cost into one running total; expectation as the flat line you'd draw through many noisy measurements.

Why the topic needs it. The proof computes . Both symbols are load-bearing: combines all buckets, averages away the randomness so we get one clean bound.


Prerequisite map

Array and its size n

Address formula floor n times x

Half-open interval 0 to 1

Floor rounds down

Subscripts x_i and n_i

Sum over all buckets

Scatter step

Uniform distribution

Even bucket loads

Binomial n and 1 over n

Expectation E

Big-O linear time


Equipment checklist

Cover the right side; can you answer before revealing?

What does stand for in bucket sort?
The number of input values — and also the number of buckets we create.
Is inside the interval ?
No — the round bracket ) excludes 1; only values up to but not reaching 1 are allowed.
Compute .
— floor rounds down, never up.
Is the same as rounding?
No — floor always goes down (), rounding can go up ().
What does the subscript in mean?
"Bucket number " — is how many items landed in that specific bucket.
Which bucket does go to when ?
.
Why multiply by before flooring?
To stretch up to so each width- value slot becomes one whole bucket.
In words, what does "uniform distribution" guarantee about buckets?
Every bucket is equally likely, so loads come out roughly even — about one item each.
What does mean compared to ?
Linear vs quadratic — doubling input doubles time but quadruples time.
What does compute?
The total per-bucket sorting work, summed over every bucket.
What is in plain words?
The long-run average value of the random quantity over many trials.