Skip to content

Hash table with double hashing

storing

Level 2 of 3

Also called Double hashing and Open addressing with a second hash.

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.

  1. step 9 of 13

    01234561710

    24 leaves 3 when divided by 7, so its home is slot 3.

    slots looked at: 0

  2. step 10 of 13

    01234561710

    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

  3. step 11 of 13

    01234561710

    Slot 3 holds 17, not 24.

    slots looked at: 1

  4. step 12 of 13

    0123456241710

    Slot 1 is free, so put 24 there.

    slots looked at: 2

Operation 3 puts 24 in, after 17 and 10. Its home, slot 3, holds 17. Its step is 5, and 3 + 5 is 8, which leaves 1 after dividing by 7, so it goes in slot 1. 2 probes.
putnumberstepits walkprobesone slot on each time, as linear probing
134313
21013, 423, 4
31733, 623, 4, 5
42453, 123, 4, 5, 6
53123, 523, 4, 5, 6, 0
Five numbers with one home, 3. Each moves on by its own step, so each passes only slot 3: 9 probes. Walking one slot on each time, as linear probing does, costs 1 + 2 + 3 + 4 + 5, which is 15.
the tableslot 3slot 1a search for 24a put of 24
17, 24 put in1724found, 2 probesalready in
17 taken, a marker left×24walks on: found, 2 probeswalks past ×: already in
17 taken, slot emptiedempty24stops: missing, 1 probestops: 24 twice
a put that stopped at ××24puts 24 in slot 3: twice
17 and 24, both home 3, fill slots 3 and 1. An empty slot ends a walk, so emptying 17's slot would hide 24. A put walks past a marker, as its number may be further along.
Putting in and taking out: 7 moments of its 27 steps, the steps between them left for the watch page
  1. step 3 of 27

    0123456

    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

    watch from here

  2. step 8 of 27

    01234561710

    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

    watch from here

  3. step 12 of 27

    0123456241710

    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

    watch from here

  4. step 17 of 27

    0123456×1710

    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

    watch from here

  5. step 21 of 27

    0123456×1710

    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

    watch from here

  6. step 25 of 27

    0123456×1710

    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

    watch from here

  7. step 26 of 27

    0123456591710

    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

    watch from here

the steps, in plain words
  1. start with 7 empty slotsPython line 12
  2. a number's home is the remainder it leaves when divided by 7Python line 14
  3. its step is 1 more than the remainder it leaves when divided by 5Python line 15
  4. look at its home slot, then move on by its step each time, round to the startPython lines 18, 19 and 20
  5. 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
  6. to take it out, stop at it and leave a markerPython lines 30 and 31
  7. 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

slope 1102104106108101102103104105106107slope 1101102103104105106107108101102103104105106107slope 1101102103104105106107108101102103104105106107

nn, 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 nnsolid where measured by running it; dashed where its formula carries it on to ten million
what the dearest chain costs, at a billion probes a second
nnslots looked attime
1,0006,979about 7 microseconds
100,000699,979about 700 microseconds
10,000,00069,999,979about 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.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
besthomes all different while there is room, 1 to nnaverageeach 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 repeatsworstthe dearest chain, each put passing every number before it
slots looked atnn in all up to 7 numbers, then 7n−427n - 42between the two, about 665 at 1007n−217n - 21 from 7 numbers on
how the slots looked at grow in each case
tight, Θ\ThetaΘ(n)\Theta(n)Θ(n)\Theta(n)Θ(n)\Theta(n)
ceiling, OOO(n)O(n)O(n)O(n)O(n)O(n)
floor, Ω\OmegaΩ(n)\Omega(n)Ω(n)\Omega(n)Ω(n)\Omega(n)

extra memory: 7 slots, nothing else

Extra memory is the room it needs on top of the input itself, while it runs.

putthe dearest chainhomes all differentat random, on averagelinear probing, at random
11111
221about 1.14about 1.14
331about 1.33about 1.35
441about 1.59about 1.65
551about 1.98about 2.10
661about 2.64about 2.82
771about 3.994
8th on7777
The probes of each put as the table fills. At random, numbers sharing a home go different ways, so from the 3rd put on a walk is a little shorter than linear probing's. Both are worked out over every choice of homes for 7, 14, 21 and on, each plus its home, as 14 + 3 is 17, home 3 and step 3. That is seven 7s multiplied together, 823,543 choices. The 2nd put lands on the 1st number's home 1 time in 7 and then probes 2, so it averages 1 and 1 seventh, about 1.14.
probesnumbers put in020406080051015probesnumbers put in020406080051015
  • the dearest chain, 7n−217n - 21 from 7 on
  • homes all different, 7n−427n - 42 from 7 on
At 7 the table is full. Up to 7, homes all different climb 1 probe a number, and the dearest chain climbs 1, then 2, then 3 and on; after 7, both climb 7 a number.
stepits walk from home 0
10, 1, 2, 3, 4, 5, 6
20, 2, 4, 6, 1, 3, 5
30, 3, 6, 2, 5, 1, 4
40, 4, 1, 5, 2, 6, 3
50, 5, 3, 1, 6, 4, 2
2, in 6 slots0, 2, 4, 0, 2, 4
Each step, 1 to 5, meets all 7 slots once in 7 probes. From another home, each slot moves on by the home, round to the start, so the walk still meets all 7. From home 3, step 2 walks 3, 5, 0, 2, 4, 6, 1. In 6 slots, step 2 shares the factor 2 with 6, and comes back to 0 after 3 probes.

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.

Why does the dearest chain cost 7n−217n - 21 probes from 7 numbers on, the most any nn different numbers can?
  1. the move: price one put

    the line:
    on the chain, the kth putprobes k slots, up to 7\begin{gathered} \text{on the chain, the } k\text{th put} \\ \text{probes } k \text{ slots, up to 7} \end{gathered}
    putnumber, home, stepits walkprobes
    11, 1, 211
    28, 1, 41, 52
    312, 5, 35, 1, 43
    418, 4, 44, 1, 5, 24
    52, 2, 32, 5, 1, 4, 05
    628, 0, 40, 4, 1, 5, 2, 66
    725, 4, 14, 5, 6, 0, 1, 2, 37
    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 kkth meets the numbers before it, one fewer than kk, then a free slot.

  2. the move: the table is full

    the line:
    from the 8th put on, each putprobes all 7 and is turned away\begin{gathered} \text{from the 8th put on, each put} \\ \text{probes all 7 and is turned away} \end{gathered}

    why it is allowed: The 7 slots hold 7 numbers, so the walk meets every slot and finds none free.

  3. the move: no numbers cost more

    the line:
    for any numbers, none repeated,the kth put probes at most kslots, and at most 7\begin{gathered} \text{for any numbers, none repeated,} \\ \text{the } k\text{th put probes at most } k \\ \text{slots, and at most 7} \end{gathered}

    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 kk, then stops at a free slot.

  4. the move: add the puts

    the line:
    W=(1+2+⋯+7)+7(n−7)W = (1 + 2 + \dots + 7) + 7(n - 7)

    why it is allowed: WW 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.

  5. the move: use the page on adding up

    the line:
    W=7×82+7(n−7)W = \frac{7 \times 8}{2} + 7(n - 7)
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      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 nn 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 n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    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.

  6. the move: work out the staircase

    the line:
    W=28+7(n−7)W = 28 + 7(n - 7)

    why it is allowed: In line 5, 7 times 8 is 56, and halved, 28. For 8, 28 + 7 is 35.

  7. the move: multiply out

    the line:
    W=28+7n−49W = 28 + 7n - 49

    why it is allowed: In line 6, 7(n−7)7(n - 7) is 7 times nn, less 7 times 7, which is 49. For 8, 28 + 56 less 49 is 35.

  8. the move: add the plain numbers

    the line:
    W=7n−21W = 7n - 21

    why it is allowed: In line 7, 28 take away 49 is minus 21. For 8, 56 less 21 is 35.

  9. the move: a ceiling

    the line:
    W≤7nW \le 7n

    why it is allowed: Line 8 takes 21 away from 7n7n. For 7, 28 is at most 49.

  10. the move: read it as a bound

    the line:
    W∈O(n)W \in O(n)

    why it is allowed: By line 9, with the fixed multiple 7.

  11. the move: a fact about n

    the line:
    21≤3n for n≥721 \le 3n \text{ for } n \ge 7

    why it is allowed: For a floor, swap the 21 for a multiple of nn. 3 times 7 is 21, and 3n3n only grows. For 8, 21 is at most 24.

  12. the move: a floor

    the line:
    W≥4n for n≥7W \ge 4n \text{ for } n \ge 7

    why it is allowed: In line 8, 7n7n less 21 is at least 7n7n less 3n3n, by line 11, which is 4n4n. For 7, 28 is at least 28.

  13. the move: read it as a bound

    the line:
    W∈Ω(n)W \in \Omega(n)
    probesnumbers put in05010015005101520probesnumbers put in05010015005101520
    • the ceiling, 7n7n
    • WW, the dearest chain
    • the floor, 4n4n
    From 7 numbers on, WW stays between 4n4n and 7n7n, 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.

  14. the move: ceiling and floor together

    the line:
    W∈Θ(n)W \in \Theta(n)

    why it is allowed: Lines 10 and 13 bound WW by the same growth, nn.

try it with a small nn: at n=7n = 7, line 4 gives W=28W = 28, which you can work out by hand

Why do the numbers 1 to nn cost the fewest probes, 7n−427n - 42 from 7 numbers on?
  1. the move: price the first 7

    the line:
    homes all different, each ofthe first 7 puts probes 1 slot\begin{gathered} \text{homes all different, each of} \\ \text{the first 7 puts probes 1 slot} \end{gathered}
    1. step 8 of 11

      012345612

      3 leaves 3 when divided by 7, so its home is slot 3.

      slots looked at: 0

    2. step 9 of 11

      012345612

      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

    3. step 10 of 11

      0123456123

      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.

  2. the move: no numbers cost less

    the line:
    every put probes at least 1 slot,and from the 8th on, 7\begin{gathered} \text{every put probes at least 1 slot,} \\ \text{and from the 8th on, 7} \end{gathered}

    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.

  3. the move: add the puts

    the line:
    B=7+7(n−7)B = 7 + 7(n - 7)

    why it is allowed: BB is the probes of 1 to nn put in, from 7 numbers on. By lines 1 and 2, 7 puts of 1, then 7 each. For 8, 7 + 7 is 14.

  4. the move: multiply out

    the line:
    B=7+7n−49B = 7 + 7n - 49

    why it is allowed: In line 3, 7(n−7)7(n - 7) is 7 times nn, less 7 times 7, which is 49. For 8, 7 + 56 less 49 is 14.

  5. the move: add the plain numbers

    the line:
    B=7n−42B = 7n - 42

    why it is allowed: In line 4, 7 take away 49 is minus 42. For 8, 56 less 42 is 14.

  6. the move: a ceiling

    the line:
    B≤7nB \le 7n

    why it is allowed: Line 5 takes 42 away from 7n7n. For 7, 7 is at most 49.

  7. the move: read it as a bound

    the line:
    B∈O(n)B \in O(n)

    why it is allowed: By line 6, with the fixed multiple 7.

  8. the move: a fact about n

    the line:
    42≤6n for n≥742 \le 6n \text{ for } n \ge 7

    why it is allowed: For a floor, swap the 42 for a multiple of nn. 6 times 7 is 42, and 6n6n only grows. For 8, 42 is at most 48.

  9. the move: a floor

    the line:
    B≥n for n≥7B \ge n \text{ for } n \ge 7

    why it is allowed: In line 5, 7n7n less 42 is at least 7n7n less 6n6n, by line 8, which is nn. For 7, 7 is at least 7.

  10. the move: read it as a bound

    the line:
    B∈Ω(n)B \in \Omega(n)

    why it is allowed: By line 9, from 7 numbers on.

  11. the move: ceiling and floor together

    the line:
    B∈Θ(n)B \in \Theta(n)

    why it is allowed: Lines 7 and 10 bound BB by the same growth, nn.

try it with a small nn: at n=7n = 7, line 3 gives B=7B = 7, which you can work out by hand

Here let nn count slots, not numbers put in. A table of nn slots has one free slot. A put of a new number has a step that is the same whatever its home. Its walk meets every slot once before any twice, as each step 1 to 5 does in 7 slots. How many probes does it make, on average over its home?
  1. the move: walk to the free slot

    the line:
    the put probes from its hometo the free slot: 1 up to n\begin{gathered} \text{the put probes from its home} \\ \text{to the free slot: 1 up to } n \end{gathered}
    homeits walkprobes
    551
    22, 52
    66, 2, 53
    33, 6, 2, 54
    00, 3, 6, 2, 55
    44, 0, 3, 6, 2, 56
    11, 4, 0, 3, 6, 2, 57
    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 nn. Its home is any of the nn slots, each as likely. This prices a put when one slot is left.

  2. the move: average over the homes

    the line:
    L=1+2+⋯+nnL = \frac{1 + 2 + \dots + n}{n}

    why it is allowed: LL is the probes averaged over the nn homes. By line 1, the homes give 1 up to nn, one each. For 7, 28 over 7.

  3. the move: use the page on adding up

    the line:
    L=n(n+1)2nL = \frac{\frac{n(n + 1)}{2}}{n}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      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 nn 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 n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    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 nn. For 7, 7 times 8, halved, is 28.

  4. the move: put the two divisions under one line

    the line:
    L=n(n+1)2nL = \frac{n(n + 1)}{2n}

    why it is allowed: Halved, then over nn, is over twice nn. For 7, 7 times 8 over 14 is 4.

  5. the move: tidy

    the line:
    L=n+12L = \frac{n + 1}{2}

    why it is allowed: The nn 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.

try it with a small nn: at n=7n = 7, line 2 gives L=4L = 4, which you can work out by hand

Numbers whose homes land at random. How many probes do they cost on average?
  1. the move: no fewer than the cheapest

    the line:
    A≥7n−42A \ge 7n - 42
    numbersthe dearest chainat random, on averagehomes all different
    728about 13.687
    100679about 665658
    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: AA 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.

  2. the move: read it as a bound

    the line:
    A∈Ω(n)A \in \Omega(n)

    why it is allowed: By line 1, as 7n−427n - 42 is at least nn once 6n6n reaches 42, from 7 numbers on (line 9 of the question on the fewest probes). For 7, 7 is at least 7.

  3. the move: no more than the dearest

    the line:
    A≤7n−21A \le 7n - 21

    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 7n−217n - 21, so no choice, nor the average, costs more.

  4. the move: read it as a bound

    the line:
    A∈O(n)A \in O(n)

    why it is allowed: By line 3, as 7n−217n - 21 is at most 7n7n, the fixed multiple 7. For 7, 28 is at most 49.

  5. the move: ceiling and floor together

    the line:
    A∈Θ(n)A \in \Theta(n)

    why it is allowed: Lines 2 and 4 bound AA by the same growth, nn.

try it on small inputs: over every choice of home for each of 7 numbers, 823,543 in all, the average AA 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.

1, 8, 12, 18, 2, 28, 25, every step written down
stepthe slots, numbers onlywhat happensslots looked at
1Start with 7 empty slots, and 7 operations to do.0
21 leaves 1 when divided by 7, so its home is slot 1.0
31 leaves 1 when divided by 5, so its step is 2: each move goes 2 slots on, round to the start.0
41Slot 1 is free, so put 1 there.11
518 leaves 1 when divided by 7, so its home is slot 1.11
618 leaves 3 when divided by 5, so its step is 4: each move goes 4 slots on, round to the start.11
71Slot 1 holds 1, not 8.12
81, 8Slot 5 is free, so put 8 there.1, 83
91, 812 leaves 5 when divided by 7, so its home is slot 5.1, 83
101, 812 leaves 2 when divided by 5, so its step is 3: each move goes 3 slots on, round to the start.1, 83
111, 8Slot 5 holds 8, not 12.1, 84
121, 8Slot 1 holds 1, not 12.1, 85
131, 12, 8Slot 4 is free, so put 12 there.1, 12, 86
141, 12, 818 leaves 4 when divided by 7, so its home is slot 4.1, 12, 86
151, 12, 818 leaves 3 when divided by 5, so its step is 4: each move goes 4 slots on, round to the start.1, 12, 86
161, 12, 8Slot 4 holds 12, not 18.1, 12, 87
171, 12, 8Slot 1 holds 1, not 18.1, 12, 88
181, 12, 8Slot 5 holds 8, not 18.1, 12, 89
191, 18, 12, 8Slot 2 is free, so put 18 there.1, 18, 12, 810
201, 18, 12, 82 leaves 2 when divided by 7, so its home is slot 2.1, 18, 12, 810
211, 18, 12, 82 leaves 2 when divided by 5, so its step is 3: each move goes 3 slots on, round to the start.1, 18, 12, 810
221, 18, 12, 8Slot 2 holds 18, not 2.1, 18, 12, 811
231, 18, 12, 8Slot 5 holds 8, not 2.1, 18, 12, 812
241, 18, 12, 8Slot 1 holds 1, not 2.1, 18, 12, 813
251, 18, 12, 8Slot 4 holds 12, not 2.1, 18, 12, 814
262, 1, 18, 12, 8Slot 0 is free, so put 2 there.2, 1, 18, 12, 815
272, 1, 18, 12, 828 leaves 0 when divided by 7, so its home is slot 0.2, 1, 18, 12, 815
282, 1, 18, 12, 828 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, 815
292, 1, 18, 12, 8Slot 0 holds 2, not 28.2, 1, 18, 12, 816
302, 1, 18, 12, 8Slot 4 holds 12, not 28.2, 1, 18, 12, 817
312, 1, 18, 12, 8Slot 1 holds 1, not 28.2, 1, 18, 12, 818
322, 1, 18, 12, 8Slot 5 holds 8, not 28.2, 1, 18, 12, 819
332, 1, 18, 12, 8Slot 2 holds 18, not 28.2, 1, 18, 12, 820
342, 1, 18, 12, 8, 28Slot 6 is free, so put 28 there.2, 1, 18, 12, 8, 2821
352, 1, 18, 12, 8, 2825 leaves 4 when divided by 7, so its home is slot 4.2, 1, 18, 12, 8, 2821
362, 1, 18, 12, 8, 2825 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, 2821
372, 1, 18, 12, 8, 28Slot 4 holds 12, not 25.2, 1, 18, 12, 8, 2822
382, 1, 18, 12, 8, 28Slot 5 holds 8, not 25.2, 1, 18, 12, 8, 2823
392, 1, 18, 12, 8, 28Slot 6 holds 28, not 25.2, 1, 18, 12, 8, 2824
402, 1, 18, 12, 8, 28Slot 0 holds 2, not 25.2, 1, 18, 12, 8, 2825
412, 1, 18, 12, 8, 28Slot 1 holds 1, not 25.2, 1, 18, 12, 8, 2826
422, 1, 18, 12, 8, 28Slot 2 holds 18, not 25.2, 1, 18, 12, 8, 2827
432, 1, 18, 25, 12, 8, 28Slot 3 is free, so put 25 there.2, 1, 18, 25, 12, 8, 2828
442, 1, 18, 25, 12, 8, 28Done. The table holds 7 numbers.2, 1, 18, 25, 12, 8, 2828

In all, 28 slots looked at. The formula from section 4, 7n−217n - 21, gives W=28W = 28 at n=7n = 7: 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 inlinear probingdouble hashing
3, 10, 17, 24, 31, all home 315 probes9 probes
1 to 7, homes all different7 probes7 probes
7 at random, on averageabout 14.06about 13.68
Double hashing wins where numbers bunch at one home. Homes all different cost the same either way.
table fulllinear probing, measuredno clustering, worked out
half2.52.0
nine tenths48.010
The books' probes for a number not there, on 4,096 slots (volume two, chapter 18). Linear probing's filled slots bunch into runs, clustering, and a walk that lands in a run walks on to its end. 'No clustering' gives every key its own order of slots. Double hashing's 'measured cost is very near' that column.