3.6.6 · D1Sorting & Searching

Foundations — Counting sort — O(n + k), integer keys only

1,698 words8 min readBack to topic

This page assumes nothing. Before you touch the parent note, every letter, box and arrow it uses is built here from the ground up. Read top to bottom — each idea is the soil the next one grows in.


0. What is an array, really?

Figure — Counting sort — O(n + k), integer keys only

Two things an array lets you do, and we will lean on both:

  • Read/write by address in one step. "Put in box " is instant — you don't walk the row looking for box 3, you jump there. This is called random access and costs (we define below).
  • Use a value as an address. If a value happens to be a small whole number like , we are allowed to say "go to box ." This tiny trick is the entire secret of counting sort — a value becomes an address.

1. The symbol — how many things

Picture it as the length of the input row. Every "scan the input once" in the parent costs about steps — one visit per box. We need a name for that count so we can talk about speed.


2. The symbol — how big the values get

Figure — Counting sort — O(n + k), integer keys only

Why does the topic need a separate letter from ? Because (how many items) and (how spread out their values are) are independent. You can have items whose values are and — tiny , huge . The whole "when is counting sort a good idea?" question is really "is small compared to ?"


3. The letter — a stand-in for "some value"

Picture as a finger pointing at one particular value while we discuss it.


4. A key vs an element

Picture a luggage tag: the number on the tag is the key, the suitcase is the element. Counting sort only ever reads the tag number to decide where the suitcase goes. This is why the parent can sort records, not just bare numbers.


5. Counting occurrences → the count array

Figure — Counting sort — O(n + k), integer keys only

Why the topic needs it: to place a value you must first know how many copies of it (and everything below it) exist. Step 1 gathers that raw information.


6. Prefix sum — turning tallies into positions

This is the cleverest borrowed tool, so we build it slowly.

Figure — Counting sort — O(n + k), integer keys only

Why this tool and not another? We need, for every value, the number of smaller-or-equal elements. A prefix sum is the one pass that computes all those running totals at once — no repeated re-adding.


7. Stability — keeping ties in order

Figure — Counting sort — O(n + k), integer keys only

Picture two suitcases both tagged : one labelled b, one labelled d, with b originally in front. A stable sort guarantees b still sits before d afterwards. This is why the parent scans the input right-to-left when placing — so the last equal element grabs the last slot and order is preserved. Stability is what lets counting sort be reused digit-by-digit inside Radix Sort.


8. Big-O — the language of "how the cost grows"

Why the topic needs it: we want to say counting sort is fast without pinning it to one computer's clock. captures the shape: cheap when is small, ruinous when explodes.


How these foundations feed the topic

Arrays and index 0 to k

Value used as an address

Count array of size k plus 1

n = how many items

Cost of a scan

k = biggest value

Prefix sum makes positions

Sum notation sigma

Exact final slot for each value

Stability keeps ties in order

Big-O gives O of n plus k

Counting Sort


Equipment checklist

Cover the right side and answer aloud. If any stalls you, re-read its section above.

Why can an array read box instantly?
Random access — the address jumps straight there in , no searching.
How many boxes cover values through ?
boxes (you must count the ).
What does stand for, and what does stand for?
= number of elements; = the maximum key value.
Can be tiny while is huge?
Yes — a few items can still hold enormously large values; that's exactly when counting sort is a bad idea.
What is a "key" versus an "element"?
The key is the number we sort by (the tag); the element is the whole record (the suitcase) carrying it.
What does count[v] hold before the prefix sum?
The number of elements whose key equals exactly .
What does mean in plain words?
Add up all tallies from box through box .
What does count[v] hold after the prefix sum?
The number of elements with key — which doubles as a position boundary.
What makes a sort "stable"?
Equal keys keep their original relative order.
What does describe?
How the running time grows: one pass over items plus one over the boxes, constants ignored.

Ready? Return to Counting sort — O(n + k), integer keys only and the derivation will read like plain English.