Hash table with double hashing
how it works
The idea in a few sentences and three pictures, then one example, moment by moment.
A hash table with double hashing keeps every number in the table's own slots, one number a slot, as open addressing does. This one has 7 slots, numbered 0 to 6. A number's home is its remainder when divided by 7: 24 is 3 sevens and 3 over, so home 3. Its step is 1 more than its remainder when divided by 5: 24 leaves 4, so step 5. If its home is taken, it moves on by its step, round from slot 6 to slot 0, until a free slot. That second remainder is double hashing. Two numbers with the same home go different ways at the first move, unless their steps agree, as Sebastian Bourges' books say (volume two, chapter 18). A take walks the same way and leaves a marker: emptied, 17's slot would stop a search for 24, as the third picture shows. A put walks past a marker, in case its number is further along. If it is not, the number goes where the first marker was. The example after these pictures puts in 17, 10 and 24, takes 24 out, looks for 31, which is not there, then puts in 59.
step 9 of 13
24 leaves 3 when divided by 7, so its home is slot 3.
slots looked at: 0
step 10 of 13
24 leaves 4 when divided by 5, so its step is 5: each move goes 5 slots on, round to the start.
slots looked at: 0
step 11 of 13
Slot 3 holds 17, not 24.
slots looked at: 1
step 12 of 13
Slot 1 is free, so put 24 there.
slots looked at: 2
| put | number | step | its walk | probes | one slot on each time, as linear probing |
|---|---|---|---|---|---|
| 1 | 3 | 4 | 3 | 1 | 3 |
| 2 | 10 | 1 | 3, 4 | 2 | 3, 4 |
| 3 | 17 | 3 | 3, 6 | 2 | 3, 4, 5 |
| 4 | 24 | 5 | 3, 1 | 2 | 3, 4, 5, 6 |
| 5 | 31 | 2 | 3, 5 | 2 | 3, 4, 5, 6, 0 |
| the table | slot 3 | slot 1 | a search for 24 | a put of 24 |
|---|---|---|---|---|
| 17, 24 put in | 17 | 24 | found, 2 probes | already in |
| 17 taken, a marker left | × | 24 | walks on: found, 2 probes | walks past ×: already in |
| 17 taken, slot emptied | empty | 24 | stops: missing, 1 probe | stops: 24 twice |
| a put that stopped at × | × | 24 | puts 24 in slot 3: twice |
step 3 of 27
17 leaves 2 when divided by 5, so its step is 3: each move goes 3 slots on, round to the start.
what it remembers to know where it is
- doing operation
- 1
- number
- 17
- step
- 3
- slot
- 3
step 8 of 27
Slot 4 is free, so put 10 there.
what it remembers to know where it is
- doing operation
- 2
- number
- 10
- step
- 1
- slot
- 4
step 12 of 27
Slot 1 is free, so put 24 there.
what it remembers to know where it is
- doing operation
- 3
- number
- 24
- step
- 5
- slot
- 1
step 17 of 27
Take 24 out of slot 1, and leave a marker so later searches walk past it.
what it remembers to know where it is
- doing operation
- 4
- number
- 24
- step
- 5
- slot
- 1
step 21 of 27
Slot 5 is empty, so the search ends. 31 is not in the table.
what it remembers to know where it is
- doing operation
- 5
- number
- 31
- step
- 2
- slot
- 5
step 25 of 27
Slot 1 holds a marker left by a number taken out. Note it: 59 can go here if it is nowhere further along.
what it remembers to know where it is
- doing operation
- 6
- number
- 59
- step
- 5
- slot
- 1
step 26 of 27
59 is nowhere further along, so put it in slot 1, where the first marker was.
what it remembers to know where it is
- doing operation
- 6
- number
- 59
- step
- 5
- slot
- 1
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- start with 7 empty slotsPython line 12
- a number's home is the remainder it leaves when divided by 7Python line 14
- its step is 1 more than the remainder it leaves when divided by 5Python line 15
- look at its home slot, then move on by its step each time, round to the startPython lines 18, 19 and 20
- to put it in, stop at a free slot or use the first marker passed; a repeat, or no room, is turned awayPython lines 16, 23, 24, 25, 26, 27, 28 and 29
- to take it out, stop at it and leave a markerPython lines 30 and 31
- an empty slot, or seven looks, ends the search, and the number is not therePython lines 20, 21 and 22
the same in Python
The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.
class Marker:
"""What a take leaves in its slot: no number, yet not empty."""
def hash_table_double_hashing(operations):
"""Run a list of operations on a hash table of 7 slots, one number each.
Each operation is a pair: ('put', n) puts n in, and ('take', n) takes n
out. Returns the numbers held, slot by slot. Each probe looks at one
slot and compares what it holds with n.
"""
slots = [None] * 7 # 7 empty slots, numbered 0 to 6
for action, number in operations:
home = number % 7 # its remainder when divided by 7
step = 1 + number % 5 # 1 more than its remainder by 5
marker = None # the first marker a put passes
stop = None # the slot that ends the search
for i in range(7): # home, then on by its step, round to the start
s = (home + i * step) % 7
if slots[s] == number or slots[s] is None: # one probe
stop = s
break
if isinstance(slots[s], Marker) and marker is None:
marker = s
if action == 'put':
if stop is not None and slots[stop] is None: # not there yet
slots[stop if marker is None else marker] = number
elif stop is None and marker is not None: # no empty slot
slots[marker] = number
elif stop is not None and slots[stop] is not None: # found it
slots[stop] = Marker() # take it out, leave a marker
return [x for x in slots if x is not None and not isinstance(x, Marker)]
how it scales
How the probes grow as more numbers go into one empty table of 7 slots, from ten numbers to a million. It is full at the 7th, so every later put is turned away after 7 probes. Three ways to pick the numbers: homes all different while there is room (1, 2, 3 and on), homes at random, and the dearest chain. In the chain, each walk passes every number before it. The chain is 1, 8, 12, 18, 2, 28 and 25, then any more, found by trying numbers from 1 to 99. The plot and tables call a probe a slot looked at, as the watch page does.
Each step across or up is ten times the one before, a log-log plot. From 100 numbers on, all three lines climb one step up for each step across: ten times the numbers, a little over ten times the probes. So the lines nearly meet at 100 numbers: 658 probes for homes all different, about 665 at random and 679 for the dearest chain. What differs is how the table fills, which the grid in section 3 shows.
slots looked at, ten times more at each mark up
, how many numbers are in the table, ten times more at each mark to the right
- the dearest chain, each put passing every number before itsolid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times n, 10 times the work
- each number's home at random, every slot as likely: the 1st number is 7 plus its home, the 2nd 14 plus its home, and on, so none repeatspoints only, each measured by a real run, since no single formula for the slots looked at of this row is worked out here
- homes all different while there is room, 1 to solid where measured by running it; dashed where its formula carries it on to ten million
| slots looked at | time | |
|---|---|---|
| 1,000 | 6,979 | about 7 microseconds |
| 100,000 | 699,979 | about 700 microseconds |
| 10,000,000 | 69,999,979 | about 70 milliseconds |
The slots looked at are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.
make these counts yourself
Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.
"""Count a hash table's probes yourself, as the plot on this page does.
Run it with Python 3.12 or later. For each size n it puts n different
numbers into an empty table of 7 slots, one at a time, and prints the
probes for 1 to n (each its own home while there is room, the cheapest),
the average over 20 tables whose numbers each have a home picked at
random, and the probes for the dearest chain, 1, 8, 12, 18, 2, 28 and 25,
then more numbers. Past 7 numbers the table is full, and each put probes
all 7 slots. A probe is one slot looked at. The counts are the same on any
computer; only the time differs.
"""
import random
class Counted:
"""A number that counts the probes made with it: one a slot looked at."""
probes = 0
def __init__(self, value):
self.value = value
def __eq__(self, other):
Counted.probes += 1
return isinstance(other, Counted) and self.value == other.value
def __mod__(self, other):
return self.value % other # finding the home and step is no probe
class Marker:
"""What a take leaves in its slot: no number, yet not empty."""
def hash_table_double_hashing(operations):
"""Run a list of operations on a hash table of 7 slots, one number each.
Each operation is a pair: ('put', n) puts n in, and ('take', n) takes n
out. Returns the numbers held, slot by slot. Each probe looks at one
slot and compares what it holds with n.
"""
slots = [None] * 7 # 7 empty slots, numbered 0 to 6
for action, number in operations:
home = number % 7 # its remainder when divided by 7
step = 1 + number % 5 # 1 more than its remainder by 5
marker = None # the first marker a put passes
stop = None # the slot that ends the search
for i in range(7): # home, then on by its step, round to the start
s = (home + i * step) % 7
if slots[s] == number or slots[s] is None: # one probe
stop = s
break
if isinstance(slots[s], Marker) and marker is None:
marker = s
if action == 'put':
if stop is not None and slots[stop] is None: # not there yet
slots[stop if marker is None else marker] = number
elif stop is None and marker is not None: # no empty slot
slots[marker] = number
elif stop is not None and slots[stop] is not None: # found it
slots[stop] = Marker() # take it out, leave a marker
return [x for x in slots if x is not None and not isinstance(x, Marker)]
def cheapest(n):
"""1 to n put in: homes 1, 2, 3, 4, 5, 6, 0, then the table is full."""
return [('put', number) for number in range(1, n + 1)]
def dearest(n):
"""The chain, each walk meeting every number before it, then 30, 31 and on."""
chain = [1, 8, 12, 18, 2, 28, 25]
return [('put', number) for number in (chain + list(range(30, 30 + n)))[:n]]
def count(operations):
"""How many probes the table makes on this list of operations."""
Counted.probes = 0
hash_table_double_hashing([(action, Counted(number)) for action, number in operations])
return Counted.probes
if __name__ == "__main__":
random.seed(1)
print(f"{'n':>6} {'own homes':>10} {'at random':>10} {'dearest':>8}")
for n in [10, 100, 1000]:
tables = []
for _ in range(20):
row = [7 * i + random.randrange(7) for i in range(1, n + 1)]
tables.append(count([('put', number) for number in row]))
print(f"{n:>6} {count(cheapest(n)):>10} {sum(tables) / 20:>10.1f} {count(dearest(n)):>8}")
best, worst and average
A put probes along its walk to the first free slot. So the homes and steps decide the cost while the table fills, and after that every put costs 7.
A case is one kind of input, the thing the method is given, such as the dearest chain, however long. A bound says how fast that case's cost grows as more numbers go in.
The counts are in the grid. A count is often the sum of pieces that grow at different speeds, and on long inputs the fastest-growing piece swamps the rest. For example, a count of half of n times n, less half of n: at 2,000 items, 2,000,000 less 1,000. So how a cost grows is what tells one method from another, and the bounds keep just that.
| besthomes all different while there is room, 1 to | averageeach number's home at random, every slot as likely: the 1st number is 7 plus its home, the 2nd 14 plus its home, and on, so none repeats | worstthe dearest chain, each put passing every number before it | |
|---|---|---|---|
| slots looked at | in all up to 7 numbers, then | between the two, about 665 at 100 | from 7 numbers on |
| how the slots looked at grow in each case | |||
| tight, | |||
| ceiling, | |||
| floor, | |||
extra memory: 7 slots, nothing else
Extra memory is the room it needs on top of the input itself, while it runs.
| put | the dearest chain | homes all different | at random, on average | linear probing, at random |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 |
| 2 | 2 | 1 | about 1.14 | about 1.14 |
| 3 | 3 | 1 | about 1.33 | about 1.35 |
| 4 | 4 | 1 | about 1.59 | about 1.65 |
| 5 | 5 | 1 | about 1.98 | about 2.10 |
| 6 | 6 | 1 | about 2.64 | about 2.82 |
| 7 | 7 | 1 | about 3.99 | 4 |
| 8th on | 7 | 7 | 7 | 7 |
- the dearest chain, from 7 on
- homes all different, from 7 on
| step | its walk from home 0 |
|---|---|
| 1 | 0, 1, 2, 3, 4, 5, 6 |
| 2 | 0, 2, 4, 6, 1, 3, 5 |
| 3 | 0, 3, 6, 2, 5, 1, 4 |
| 4 | 0, 4, 1, 5, 2, 6, 3 |
| 5 | 0, 5, 3, 1, 6, 4, 2 |
| 2, in 6 slots | 0, 2, 4, 0, 2, 4 |
where the formula comes from
Each count above, worked out line by line. Each reason names the line it uses, and each 'where does this come from?' box opens a fact from another page.
the move: price one put
the line:put number, home, step its walk probes 1 1, 1, 2 1 1 2 8, 1, 4 1, 5 2 3 12, 5, 3 5, 1, 4 3 4 18, 4, 4 4, 1, 5, 2 4 5 2, 2, 3 2, 5, 1, 4, 0 5 6 28, 0, 4 0, 4, 1, 5, 2, 6 6 7 25, 4, 1 4, 5, 6, 0, 1, 2, 3 7 Each walk meets every number put before it, then a free slot. 1 up to 7 probes, 28 in all. why it is allowed: Count the puts from 1. The table under this line walks each of the chain's 7 numbers: the th meets the numbers before it, one fewer than , then a free slot.
the move: the table is full
the line:why it is allowed: The 7 slots hold 7 numbers, so the walk meets every slot and finds none free.
guess it, thenthe move: no numbers cost more
the line:why it is allowed: A walk meets each slot at most once, by section 3's last picture, from any home. It passes only slots holding an earlier number, at most one fewer than , then stops at a free slot.
guess it, thenthe move: add the puts
the line:why it is allowed: is the probes of the dearest chain, from 7 numbers on. By lines 1 and 2, puts 1 to 7 make 1 up to 7, and each later put 7, the most line 3 allows. For 8, 28 + 7.
guess it, thenthe move: use the page on adding up
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The box under this line shows that 1 up to a last number adds to that number times one more, halved. Here the last number is 7: 7 times 8, halved, is 28.
guess it, thenthe move: work out the staircase
the line:why it is allowed: In line 5, 7 times 8 is 56, and halved, 28. For 8, 28 + 7 is 35.
guess it, thenthe move: multiply out
the line:why it is allowed: In line 6, is 7 times , less 7 times 7, which is 49. For 8, 28 + 56 less 49 is 35.
guess it, thenthe move: add the plain numbers
the line:why it is allowed: In line 7, 28 take away 49 is minus 21. For 8, 56 less 21 is 35.
guess it, thenthe move: a ceiling
the line:why it is allowed: Line 8 takes 21 away from . For 7, 28 is at most 49.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 9, with the fixed multiple 7.
guess it, thenthe move: a fact about n
the line:why it is allowed: For a floor, swap the 21 for a multiple of . 3 times 7 is 21, and only grows. For 8, 21 is at most 24.
guess it, thenthe move: a floor
the line:why it is allowed: In line 8, less 21 is at least less , by line 11, which is . For 7, 28 is at least 28.
guess it, thenthe move: read it as a bound
the line:- the ceiling,
- , the dearest chain
- the floor,
From 7 numbers on, stays between and , two lines of the same growth. At 20, 119 lies between 80 and 140. why it is allowed: By line 12, from 7 numbers on.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 10 and 13 bound by the same growth, .
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
the move: price the first 7
the line:step 8 of 11
3 leaves 3 when divided by 7, so its home is slot 3.
slots looked at: 0
step 9 of 11
3 leaves 3 when divided by 5, so its step is 4: each move goes 4 slots on, round to the start.
slots looked at: 0
step 10 of 11
Slot 3 is free, so put 3 there.
slots looked at: 1
Operation 3 puts 3 in, after 1 and 2. Its home, slot 3, is free, so its step is never used. 1 probe. why it is allowed: 1 to 7 have homes 1, 2, 3, 4, 5, 6 and 0, so each home is free when its number arrives.
the move: no numbers cost less
the line:why it is allowed: A put looks at its home at least. Any 7 different numbers fill all 7 slots, as each walk meets every slot. Then, by line 2 of the question on the dearest chain, every later put probes all 7.
guess it, thenthe move: add the puts
the line:why it is allowed: is the probes of 1 to put in, from 7 numbers on. By lines 1 and 2, 7 puts of 1, then 7 each. For 8, 7 + 7 is 14.
guess it, thenthe move: multiply out
the line:why it is allowed: In line 3, is 7 times , less 7 times 7, which is 49. For 8, 7 + 56 less 49 is 14.
guess it, thenthe move: add the plain numbers
the line:why it is allowed: In line 4, 7 take away 49 is minus 42. For 8, 56 less 42 is 14.
guess it, thenthe move: a ceiling
the line:why it is allowed: Line 5 takes 42 away from . For 7, 7 is at most 49.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 6, with the fixed multiple 7.
guess it, thenthe move: a fact about n
the line:why it is allowed: For a floor, swap the 42 for a multiple of . 6 times 7 is 42, and only grows. For 8, 42 is at most 48.
guess it, thenthe move: a floor
the line:why it is allowed: In line 5, less 42 is at least less , by line 8, which is . For 7, 7 is at least 7.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 9, from 7 numbers on.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 7 and 10 bound by the same growth, .
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: walk to the free slot
the line:home its walk probes 5 5 1 2 2, 5 2 6 6, 2, 5 3 3 3, 6, 2, 5 4 0 0, 3, 6, 2, 5 5 4 4, 0, 3, 6, 2, 5 6 1 1, 4, 0, 3, 6, 2, 5 7 7 slots, only slot 5 free, step 3. Every walk is the end of 1, 4, 0, 3, 6, 2, 5, so each home gives a different number of probes, 1 to 7. why it is allowed: Its walk meets every slot once before any twice, as the question says. Walk back from the free slot by the step: that meets each home once, so each home is a different number of probes away, 1 up to . Its home is any of the slots, each as likely. This prices a put when one slot is left.
the move: average over the homes
the line:why it is allowed: is the probes averaged over the homes. By line 1, the homes give 1 up to , one each. For 7, 28 over 7.
guess it, thenthe move: use the page on adding up
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The box under this line shows that 1 up to a last number adds to that number times one more, halved. Here the last number is . For 7, 7 times 8, halved, is 28.
guess it, thenthe move: put the two divisions under one line
the line:why it is allowed: Halved, then over , is over twice . For 7, 7 times 8 over 14 is 4.
guess it, thenthe move: tidy
the line:why it is allowed: The on top and below cancel. For 7 slots, 8 over 2 is 4. Section 3's 7th put averages about 3.99 instead, as there the step changes with the home.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: no fewer than the cheapest
the line:numbers the dearest chain at random, on average homes all different 7 28 about 13.68 7 100 679 about 665 658 At 7 numbers, the average over all 823,543 choices of homes. It is section 3's column at random, added up: 1 + 1.14 + 1.33 + 1.59 + 1.98 + 2.64 + 3.99 is 13.67. Before each was rounded, it is about 13.68. At 100, the average of 20 tables picked at random. why it is allowed: is the probes averaged over every choice of homes. No choice costs fewer than homes all different while there is room, by lines 2 and 5 of the question on the fewest probes, so neither does the average.
the move: read it as a bound
the line:why it is allowed: By line 1, as is at least once reaches 42, from 7 numbers on (line 9 of the question on the fewest probes). For 7, 7 is at least 7.
guess it, thenthe move: no more than the dearest
the line:why it is allowed: No put costs more than the chain's, by line 3 of the question on the dearest chain. Line 4 there adds those up, and line 8 tidies them to , so no choice, nor the average, costs more.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 3, as is at most , the fixed multiple 7. For 7, 28 is at most 49.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 2 and 4 bound by the same growth, .
guess it, then
try it on small inputs: over every choice of home for each of 7 numbers, 823,543 in all, the average is about 13.68
count it yourself
Count every probe as 1, 8, 12, 18, 2, 28 and 25 go into an empty table. Each walk meets every number before it.
| step | the slots, numbers only | what happens | slots looked at |
|---|---|---|---|
| 1 | Start with 7 empty slots, and 7 operations to do. | 0 | |
| 2 | 1 leaves 1 when divided by 7, so its home is slot 1. | 0 | |
| 3 | 1 leaves 1 when divided by 5, so its step is 2: each move goes 2 slots on, round to the start. | 0 | |
| 4 | 1 | Slot 1 is free, so put 1 there.1 | 1 |
| 5 | 1 | 8 leaves 1 when divided by 7, so its home is slot 1.1 | 1 |
| 6 | 1 | 8 leaves 3 when divided by 5, so its step is 4: each move goes 4 slots on, round to the start.1 | 1 |
| 7 | 1 | Slot 1 holds 1, not 8.1 | 2 |
| 8 | 1, 8 | Slot 5 is free, so put 8 there.1, 8 | 3 |
| 9 | 1, 8 | 12 leaves 5 when divided by 7, so its home is slot 5.1, 8 | 3 |
| 10 | 1, 8 | 12 leaves 2 when divided by 5, so its step is 3: each move goes 3 slots on, round to the start.1, 8 | 3 |
| 11 | 1, 8 | Slot 5 holds 8, not 12.1, 8 | 4 |
| 12 | 1, 8 | Slot 1 holds 1, not 12.1, 8 | 5 |
| 13 | 1, 12, 8 | Slot 4 is free, so put 12 there.1, 12, 8 | 6 |
| 14 | 1, 12, 8 | 18 leaves 4 when divided by 7, so its home is slot 4.1, 12, 8 | 6 |
| 15 | 1, 12, 8 | 18 leaves 3 when divided by 5, so its step is 4: each move goes 4 slots on, round to the start.1, 12, 8 | 6 |
| 16 | 1, 12, 8 | Slot 4 holds 12, not 18.1, 12, 8 | 7 |
| 17 | 1, 12, 8 | Slot 1 holds 1, not 18.1, 12, 8 | 8 |
| 18 | 1, 12, 8 | Slot 5 holds 8, not 18.1, 12, 8 | 9 |
| 19 | 1, 18, 12, 8 | Slot 2 is free, so put 18 there.1, 18, 12, 8 | 10 |
| 20 | 1, 18, 12, 8 | 2 leaves 2 when divided by 7, so its home is slot 2.1, 18, 12, 8 | 10 |
| 21 | 1, 18, 12, 8 | 2 leaves 2 when divided by 5, so its step is 3: each move goes 3 slots on, round to the start.1, 18, 12, 8 | 10 |
| 22 | 1, 18, 12, 8 | Slot 2 holds 18, not 2.1, 18, 12, 8 | 11 |
| 23 | 1, 18, 12, 8 | Slot 5 holds 8, not 2.1, 18, 12, 8 | 12 |
| 24 | 1, 18, 12, 8 | Slot 1 holds 1, not 2.1, 18, 12, 8 | 13 |
| 25 | 1, 18, 12, 8 | Slot 4 holds 12, not 2.1, 18, 12, 8 | 14 |
| 26 | 2, 1, 18, 12, 8 | Slot 0 is free, so put 2 there.2, 1, 18, 12, 8 | 15 |
| 27 | 2, 1, 18, 12, 8 | 28 leaves 0 when divided by 7, so its home is slot 0.2, 1, 18, 12, 8 | 15 |
| 28 | 2, 1, 18, 12, 8 | 28 leaves 3 when divided by 5, so its step is 4: each move goes 4 slots on, round to the start.2, 1, 18, 12, 8 | 15 |
| 29 | 2, 1, 18, 12, 8 | Slot 0 holds 2, not 28.2, 1, 18, 12, 8 | 16 |
| 30 | 2, 1, 18, 12, 8 | Slot 4 holds 12, not 28.2, 1, 18, 12, 8 | 17 |
| 31 | 2, 1, 18, 12, 8 | Slot 1 holds 1, not 28.2, 1, 18, 12, 8 | 18 |
| 32 | 2, 1, 18, 12, 8 | Slot 5 holds 8, not 28.2, 1, 18, 12, 8 | 19 |
| 33 | 2, 1, 18, 12, 8 | Slot 2 holds 18, not 28.2, 1, 18, 12, 8 | 20 |
| 34 | 2, 1, 18, 12, 8, 28 | Slot 6 is free, so put 28 there.2, 1, 18, 12, 8, 28 | 21 |
| 35 | 2, 1, 18, 12, 8, 28 | 25 leaves 4 when divided by 7, so its home is slot 4.2, 1, 18, 12, 8, 28 | 21 |
| 36 | 2, 1, 18, 12, 8, 28 | 25 leaves 0 when divided by 5, so its step is 1: each move goes one slot on, round to the start.2, 1, 18, 12, 8, 28 | 21 |
| 37 | 2, 1, 18, 12, 8, 28 | Slot 4 holds 12, not 25.2, 1, 18, 12, 8, 28 | 22 |
| 38 | 2, 1, 18, 12, 8, 28 | Slot 5 holds 8, not 25.2, 1, 18, 12, 8, 28 | 23 |
| 39 | 2, 1, 18, 12, 8, 28 | Slot 6 holds 28, not 25.2, 1, 18, 12, 8, 28 | 24 |
| 40 | 2, 1, 18, 12, 8, 28 | Slot 0 holds 2, not 25.2, 1, 18, 12, 8, 28 | 25 |
| 41 | 2, 1, 18, 12, 8, 28 | Slot 1 holds 1, not 25.2, 1, 18, 12, 8, 28 | 26 |
| 42 | 2, 1, 18, 12, 8, 28 | Slot 2 holds 18, not 25.2, 1, 18, 12, 8, 28 | 27 |
| 43 | 2, 1, 18, 25, 12, 8, 28 | Slot 3 is free, so put 25 there.2, 1, 18, 25, 12, 8, 28 | 28 |
| 44 | 2, 1, 18, 25, 12, 8, 28 | Done. The table holds 7 numbers.2, 1, 18, 25, 12, 8, 28 | 28 |
In all, 28 slots looked at. The formula from section 4, , gives at : the same.
where you meet it
Double hashing is in tables that give each key its own step.
.NET's Hashtable
Hashtable, the older table of .NET, keeps its keys, the things it looks up, by open addressing. A second hash of each key gives its step, so two keys with one home walk apart, unless their steps agree.
source: .NET's source, dotnet/runtime, System.Private.CoreLib, System/Collections/Hashtable.cs, InitHash
GCC's libiberty
The hash table GCC's own tools share looks up a key by open addressing. A hash is a number made from the key. When its first slot is taken, the step is 1 more than the remainder of its hash divided by 2 less than the size. In 7 slots, that is by 5, as here.
source: GCC's source, libiberty/hashtab.c, htab_find_slot_with_hash and htab_mod_m2
what is used instead
Many tables walk one slot on each time instead, as open addressing's page does. Sebastian Bourges' books compare three walks: one slot on, a jump that grows, and double hashing. Linear probing, 'the theoretically worst of the three, is the one most high-performance tables actually use' (volume two, chapter 18). A computer reads slots side by side together, so its longer walks still run quickly.
how it compares
Double hashing beside linear probing, on this site's numbers and in the books' table.
| numbers put in | linear probing | double hashing |
|---|---|---|
| 3, 10, 17, 24, 31, all home 3 | 15 probes | 9 probes |
| 1 to 7, homes all different | 7 probes | 7 probes |
| 7 at random, on average | about 14.06 | about 13.68 |
| table full | linear probing, measured | no clustering, worked out |
|---|---|---|
| half | 2.5 | 2.0 |
| nine tenths | 48.0 | 10 |