Hashing a show in 8 chapters
Goal: find any record in about one look now about 501 looks on average
1 of 8 Your guess
Before we start
Python's dict, JavaScript's Map and Go's map all find a key fast. Which picture is closest to how you think they do it?
In c, a name's sum means each letter's place in the alphabet, added up: cat is c 3 + a 1 + t 20 = 24.
Pick the closest. You find out which fits at the end.
The file: 1,000 records. A look is one record checked.
501looks on average, front to back
Found halfway on average: 501 looks
1,000looks at worst, when it is last
every record checked
10looks halving a sorted file
1,000›500›250›125›63›32›16›8›4›2›1
10 cuts, each keeping the half that holds yours, odd halves rounded up
2 of 8 The problem
1956: a file too big to search
You are a records clerk. Older machines kept records on tape: to reach one, you ran the tape past from the very start. Now there is a new kind of memory that jumps straight to any address in one step. Engineers call it random-access memory.
But a jump only helps if you know which address holds the record you want. You don't, so you still search, one record at a time, and the file keeps growing.
If the file is sorted you can halve it: look at the middle record, keep the half that must hold yours, and repeat.
Grow the file and watch the three numbers.
Your goal: find any record in about one look, however big the file gets.
What changed
- Records in the file: 1,000 → 4,000
- Looks on average, front to back: 501 → 2,001
- Looks by halving: 10 → 12
That year Arnold Dumey, an engineer who had been an Army codebreaker in the war, asked why finding the address still took a search at all.
Why 501?
Front to back, the record is as likely to be first as last, so on average you check (1,000 + 1) ÷ 2 = 500.5 records. Half a look isn't a thing, so round up to a whole look: 501.
looks so far: 0
3 of 8 Try it
Be the clerk
Someone asks for the record on yak. Your 11 records went into the first 11 of 16 boxes, in the order they arrived. Find it.
Tap any box to open it, or open them in order.
Front to back is the only plan when nothing tells you where a record is. On the 1,000-record file from before, that is about 501 looks on average and 1,000 at worst.
The wall: 16 boxes, numbered 0 to 15
looks to find a record:
4 of 8 The idea
Work out the box
In 1956 Arnold Dumey, the engineer from before, had an idea: don't search. Work the box out from the name. Follow cat.
Press Play to add them up.
What changed
- Looks to find a record: 9 → 1
What is left over is the remainder, always 0 to 15. The hash: same name in, same box out.
A new record arrives: act
looks to find act:
5 of 8 The cost
Two names, one box
Sixteen boxes, and far more possible names. Sooner or later two names give the same box. What then?
Follow act.
A collision is certain once there are more names than boxes, not a bug. A box that keeps a short stack is chaining. Walking to the next free box is open addressing.
Why does sharing have to happen?
There are far more possible names than boxes. Put 17 names into 16 boxes and at least one box gets two, however good the sum is.
A third name arrives: tac = 20 + 1 + 3 = 24, so box 8, under cat and act.
Average for this box: (1 + 2 + 3) ÷ 3 = 2 looks
A wall of 65,536 boxes, filled to 0.7 per box: 0.7 × 65,536 ≈ 45,875 records
records in boxboxeslooks to find each once
123,223
28,288
31,724
4216
58
1 look per record extra looks down a stack
A stack of 2 costs 1 + 2 = 3 looks to search both, a stack of 3 costs 1 + 2 + 3 = 6.
60,711 looks ÷ 45,875 records = 1.32 looks per search
45,875 records ÷ 65,536 boxes = 0.7 per box
measured: fill the wall, search every record
0.25 per box
0.35 per box
0.5 per box
0.7 per box
1 per box
2 per box
4 per box
Each bar is the wall, its length the number of boxes.
before
65,536 boxes · 0.7 per box · 1.32 looks per search
↓ all 45,875 records re-filed once, each box worked out again
after doubling
131,072 boxes · 0.35 per box · 1.20 looks per search
what doubling costs
22,937 inserts since the last doubling
45,875 records re-filed, once
45,875 ÷ 22,937 = about 2 extra filings per insert, spread out
6 of 8 The fix
How full is too full
A box can hold a stack. To find a record you read down the stack until you reach it, so the deeper it sits, the more looks it costs.
Here is the lesson's wall, filled by a real sum. Search for every record once and add up the looks. Most boxes cost 1, and the stacks of 2 and 3 push the average above it.
Drag to fill the wall. Fuller boxes mean deeper stacks, so every search costs more. Records ÷ boxes is the load factor.
0.7 per box. This is where real tables double the wall.
Past about 0.7, real tables double the wall and re-file every record once. Each box then holds half as many, so searches get cheaper. It happens once each time the records double, so it is rare.
What changed
- Records per box: 0.7 → 0.35
- Looks per search: 1.32 → 1.20
7 of 8 Your guess
Your picture now
Goal met: 9 looks down to 1, and 1.32 on average (measured) while the wall stays under 0.7 full. Which picture is closest now?
Pick one.
c is the hash table. a is a sorted list, which a tree does well. b never happens: names always end up sharing.
8 of 8 Keep this
Keep this
Work out where a record has to be instead of looking for it. Let a box hold a few, and grow the wall before it passes 0.7 full.
The price: the boxes are in no order. For ranges or "the next largest", reach for a tree.
Read the full lessonComing up A hash table keeps its records in no order at all. Next: records kept in a row, where adding one in the middle means shoving every card after it over, and a card that says where the next one is.