Skip to content

Hash table with chaining

storing

Level 2 of 3

Also called Separate chaining.

how it works

The idea in a few sentences and two pictures, then one run, moment by moment.

A hash table works out where a number goes from the number itself. This one has 5 buckets, numbered 0 to 4. A number goes in the bucket of its remainder when divided by 5: 42 is 8 fives and 2 over, so bucket 2. A table holds each number once, so to put a number in, you compare it with each number already in its bucket, oldest first. If none is equal, it goes on the end. To search for a number or take it out, you walk its bucket the same way, up to the number. Only one bucket is ever searched. Numbers sharing a bucket wait there in a chain, so this is a hash table with chaining. This page counts looks: one number in the bucket compared with the number in hand. The run after these pictures puts in 17, 42, 8 and 22, takes 42 out, then puts in 35 and 12.

  1. step 9 of 13

    221742801234

    22 leaves 2 when divided by 5, so it belongs in bucket 2.

    compared: 0

  2. step 10 of 13

    221742801234

    17 is not 22.

    compared: 1

  3. step 11 of 13

    221742801234

    42 is not 22.

    compared: 2

  4. step 12 of 13

    174222801234

    22 is not in bucket 2 yet, so put it in.

    compared: 2

Operation 4 puts 22 in, after 17, 42 and 8. 22 leaves 2, so only bucket 2 is searched: 22 is compared with 17, then 42, and goes on the end. 2 looks, and 8 in bucket 3 is never looked at. 22 waits in hand above the buckets, yellow like each number it meets; red is just put in.
3501101722122383140slotkeyslanded where a key already was
The table at the end of the run below, oldest at the bottom; the number under each bucket counts its numbers. A search for 12 compares it with 17, 22 and 12, 3 looks. A search for 9 finds bucket 4 empty: no look at all.
Putting in and taking out: 8 moments of its 23 steps, the steps between them left for the watch page
  1. step 2 of 23

    1701234

    17 leaves 2 when divided by 5, so it belongs in bucket 2.

    what it remembers to know where it is
    doing operation
    1
    number
    17
    bucket
    2

    watch from here

  2. step 6 of 23

    174201234

    42 is not in bucket 2 yet, so put it in.

    what it remembers to know where it is
    doing operation
    2
    number
    42
    bucket
    2

    watch from here

  3. step 8 of 23

    1742801234

    8 is not in bucket 3 yet, so put it in.

    what it remembers to know where it is
    doing operation
    3
    number
    8
    bucket
    3

    watch from here

  4. step 12 of 23

    174222801234

    22 is not in bucket 2 yet, so put it in.

    what it remembers to know where it is
    doing operation
    4
    number
    22
    bucket
    2

    watch from here

  5. step 15 of 23

    42174222801234

    Here is 42, in bucket 2.

    what it remembers to know where it is
    doing operation
    5
    number
    42
    bucket
    2

    watch from here

  6. step 16 of 23

    1722801234

    Take 42 out of bucket 2.

    what it remembers to know where it is
    doing operation
    5
    number
    42
    bucket
    2

    watch from here

  7. step 18 of 23

    351722801234

    35 is not in bucket 0 yet, so put it in.

    what it remembers to know where it is
    doing operation
    6
    number
    35
    bucket
    0

    watch from here

  8. step 22 of 23

    35172212801234

    12 is not in bucket 2 yet, so put it in.

    what it remembers to know where it is
    doing operation
    7
    number
    12
    bucket
    2

    watch from here

the steps, in plain words
  1. start with 5 empty bucketsPython line 8
  2. a number's bucket is the remainder it leaves when divided by 5Python line 10
  3. compare it with each number already in that bucketPython lines 11, 12, 13, 14 and 15
  4. to put it in, add it to the bucket unless it is already therePython lines 16, 17 and 18
  5. to take it out, remove it from the bucket if it is therePython lines 19 and 20
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.

def hash_table_chaining(operations):
    """Run a list of operations on a hash table of 5 buckets.

    Each operation is a pair: ('put', n) puts n in, and ('take', n) takes n
    out. Returns the 5 buckets, each a list of its numbers, oldest first.
    Each look compares one number already in the bucket with n.
    """
    buckets = [[] for _ in range(5)]  # 5 empty buckets, numbered 0 to 4
    for action, number in operations:
        bucket = buckets[number % 5]  # its remainder when divided by 5
        found = None
        for place, other in enumerate(bucket):  # oldest first
            if other == number:  # one look
                found = place
                break
        if action == 'put':
            if found is None:  # not there yet
                bucket.append(number)  # so add it on the end
        elif found is not None:  # it is there
            del bucket[found]  # so take it out
    return buckets

how it scales

How the looks grow, from ten numbers to ten thousand, each put into an empty table of 5 buckets one at a time. Spread in turn is 1, 2, 3 and on; at random picks each bucket by chance; all in one bucket is 5, 10, 15 and on. The plot and tables call a look a number compared, 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 two steps up for each step across: ten times the numbers, a hundred times the looks. The 5 buckets never grow, so each chain grows with nn. Spread in turn starts lower, as its first 5 numbers share no bucket, then nearly meets at random: 950 and 990 looks at 100 numbers. All in one bucket costs 4,950.

compared, ten times more at each mark up

a seconda minutean houra dayslope 2102104106108101010121014101102103104105106107a seconda minutean houra dayslope 210110210310410510610710810910101011101210131014101102103104105106107a seconda minutean houra dayslope 210110210310410510610710810910101011101210131014101102103104105106107

nn, how many numbers are in the table, ten times more at each mark to the right

  • all in one bucket, 5, 10, and on up to 5n5nsolid where measured by running it; dashed where its formula carries it on to ten millionslope 2: ten times n, 100 times the work
  • each number's bucket at random, every bucket as likelysolid where measured by running it; dashed where its formula carries it on to ten million
  • spread over the buckets in turn, 1 to nnsolid where measured by running it; dashed where its formula carries it on to ten million
what numbers all in one bucket cost, at a billion looks a second
nncomparedtime
1,000499,500about 500 microseconds
100,0004,999,950,000about 5 seconds
10,000,00049,999,995,000,000about 14 hours

The compared 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 looks 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 5 buckets, one at a time, and prints the
looks for 1 to n (spread over the buckets in turn, the cheapest), the
average over 20 tables whose numbers each land in a bucket picked at
random, and the looks for 5, 10, up to 5n (all in bucket 0, the dearest).
A look is one number in the bucket compared with the number put in. The
counts are the same on any computer; only the time differs.
"""
import random


class Counted:
    """A number that counts the looks made with it: one a comparison."""

    looks = 0

    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
        Counted.looks += 1
        return self.value == other.value

    def __mod__(self, other):
        return self.value % other  # finding the bucket is no look


def hash_table_chaining(operations):
    """Run a list of operations on a hash table of 5 buckets.

    Each operation is a pair: ('put', n) puts n in, and ('take', n) takes n
    out. Returns the 5 buckets, each a list of its numbers, oldest first.
    Each look compares one number already in the bucket with n.
    """
    buckets = [[] for _ in range(5)]  # 5 empty buckets, numbered 0 to 4
    for action, number in operations:
        bucket = buckets[number % 5]  # its remainder when divided by 5
        found = None
        for place, other in enumerate(bucket):  # oldest first
            if other == number:  # one look
                found = place
                break
        if action == 'put':
            if found is None:  # not there yet
                bucket.append(number)  # so add it on the end
        elif found is not None:  # it is there
            del bucket[found]  # so take it out
    return buckets


def cheapest(n):
    """1 to n put in: the buckets fill in turn, 1, 2, 3, 4, 0, then again."""
    return [('put', number) for number in range(1, n + 1)]


def count(operations):
    """How many looks the table makes on this list of operations."""
    Counted.looks = 0
    hash_table_chaining([(action, Counted(number)) for action, number in operations])
    return Counted.looks


if __name__ == "__main__":
    random.seed(1)
    print(f"{'n':>6} {'spread in turn':>15} {'at random':>10} {'one bucket':>11}")
    for n in [10, 100, 1000]:
        tables = []
        for _ in range(20):
            row = [5 * i + random.randrange(5) for i in range(1, n + 1)]
            tables.append(count([('put', number) for number in row]))
        dearest = [('put', 5 * i) for i in range(1, n + 1)]
        print(f"{n:>6} {count(cheapest(n)):>15} {sum(tables) / 20:>10.1f} {count(dearest):>11}")

best, worst and average

A put makes a look for each number already in its bucket. So how the numbers spread over the buckets decides the cost, and their order does not.

A case is one kind of input, the thing the method is given, such as numbers that all leave the same remainder, however many. 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
bestspread over the buckets in turn, 1 to nnaverageeach number's bucket at random, every bucket as likelyworstall in one bucket, 5, 10, and on up to 5n5n
compared⌊05⌋+⌊15⌋+⋯+⌊n−15⌋\lfloor \frac{0}{5} \rfloor + \lfloor \frac{1}{5} \rfloor + \dots + \lfloor \frac{n - 1}{5} \rfloor, each fifth rounded down; 950 for 100, about a tenth of 100 times 100(n−1)n10\frac{(n - 1)n}{10}, a fifth of the dearest(n−1)n2\frac{(n - 1)n}{2}, every number each time
how the compared grow in each case
tight, Θ\ThetaΘ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)
ceiling, OOO(n2)O(n^2)O(n2)O(n^2)O(n2)O(n^2)
floor, Ω\OmegaΩ(n2)\Omega(n^2)Ω(n2)\Omega(n^2)Ω(n2)\Omega(n^2)

extra memory: 5 buckets, each a list of its numbers

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

casenumbers put intheir bucketslooks by each putlooks in all
spread in turn1, 2, 3, 4, 5, 61, 2, 3, 4, 0, 10, 0, 0, 0, 0, 11
the same, back to front6, 5, 4, 3, 2, 11, 0, 4, 3, 2, 10, 0, 0, 0, 0, 11
one at random13, 27, 8, 41, 19, 333, 2, 3, 1, 4, 30, 0, 1, 0, 0, 23
all in one bucket5, 10, 15, 20, 25, 300, 0, 0, 0, 0, 00, 1, 2, 3, 4, 515
Six numbers four ways. 1 to 6 cost 1 look in either order: only 6 and 1 share a bucket. At random, 13, 8 and 33 share bucket 3. All leaving 0, each meets every number before it.
looksnumbers put in05010015020005101520looksnumbers put in05010015020005101520
  • all in one bucket, (n−1)n2\frac{(n - 1)n}{2}
  • at random, on average, (n−1)n10\frac{(n - 1)n}{10}
  • spread in turn
Double the numbers, 10 to 20, and the looks grow about 4 times, 45 to 190 in one bucket and 9 to 38 at random. Spread in turn costs nothing up to 5 numbers, then stays below at random: 30 looks against 38 at 20.

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 do nn numbers in one bucket cost (n−1)n2\frac{(n - 1)n}{2} looks, the most any nn different numbers can?
  1. the move: price one put

    the line:
    a put makes 1 look for eachnumber already in its bucket\begin{gathered} \text{a put makes 1 look for each} \\ \text{number already in its bucket} \end{gathered}
    1. step 11 of 16

      205101501234

      20 leaves 0 when divided by 5, so it belongs in bucket 0.

      compared: 0

    2. step 12 of 16

      205101501234

      5 is not 20.

      compared: 1

    3. step 13 of 16

      205101501234

      10 is not 20.

      compared: 2

    4. step 14 of 16

      205101501234

      15 is not 20.

      compared: 3

    5. step 15 of 16

      510152001234

      20 is not in bucket 0 yet, so put it in.

      compared: 3

    Operation 4 puts 20 in, after 5, 10 and 15. All four leave 0, so 20 is compared with all three and goes on the end. 3 looks. 20 waits in hand above the buckets, yellow like each number it meets; red is just put in.

    why it is allowed: The numbers are all different, so no look finds an equal one, and the walk goes to the bucket's end.

  2. the move: one bucket, every number

    the line:
    all in one bucket, the kth putmakes k−1 looks\begin{gathered} \text{all in one bucket, the } k\text{th put} \\ \text{makes } k - 1 \text{ looks} \end{gathered}

    why it is allowed: Count the puts from 1, so the kkth put is put number kk. 5, 10, 15 and on all leave 0, so it finds the k−1k - 1 numbers before it in bucket 0. For 4, the fourth put makes 3.

  3. the move: no numbers cost more

    the line:
    for any different numbers, the kthput makes at most k−1 looks\begin{gathered} \text{for any different numbers, the } k\text{th} \\ \text{put makes at most } k - 1 \text{ looks} \end{gathered}

    why it is allowed: Its bucket holds at most the k−1k - 1 numbers already in, so by line 1 it makes at most k−1k - 1 looks. So no numbers cost more than line 2's.

  4. the move: add the puts

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

    why it is allowed: WW is the looks of nn numbers in one bucket. By line 2, puts 1 to nn make 0, 1, and on up to n−1n - 1. For 4, 0 + 1 + 2 + 3 is 6.

  5. the move: leave out the 0

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

    why it is allowed: Line 4 without its 0, as adding 0 changes nothing.

  6. the move: use the page on adding up

    the line:
    W=(n−1)((n−1)+1)2W = \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2}
    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 n−1n - 1. For 4, 1 + 2 + 3 is 3 times 4, halved, which is 6.

  7. the move: tidy

    the line:
    W=(n−1)n2W = \frac{(n - 1)n}{2}

    why it is allowed: In line 6, (n−1)+1(n - 1) + 1 is nn. For 5, 4 times 5, halved, is 10.

  8. the move: a ceiling

    the line:
    W≤n22W \le \frac{n^2}{2}

    why it is allowed: In line 7, n−1n - 1 is less than nn. For 5, 10 is at most 12.5.

  9. the move: read it as a bound

    the line:
    W∈O(n2)W \in O(n^2)

    why it is allowed: By line 8, with the fixed multiple one half.

  10. the move: a fact about n

    the line:
    n−1≥n2 for n≥2n - 1 \ge \frac{n}{2} \text{ for } n \ge 2

    why it is allowed: Taking 1 from nn takes at most half of it once nn is 2 or more. For 5, 4 is at least 2.5.

  11. the move: a floor

    the line:
    W≥n24 for n≥2W \ge \frac{n^2}{4} \text{ for } n \ge 2

    why it is allowed: In line 7, write half of nn in place of n−1n - 1, which by line 10 is no bigger. Half of nn times nn, halved, is n24\frac{n^2}{4}. For 5, 10 is at least 6.25.

  12. the move: read it as a bound

    the line:
    W∈Ω(n2)W \in \Omega(n^2)
    looksnumbers put in05010015020005101520looksnumbers put in05010015020005101520
    • the ceiling, n22\frac{n^2}{2}
    • W=(n−1)n2W = \frac{(n - 1)n}{2}
    • the floor, n24\frac{n^2}{4}
    From 2 numbers on, WW stays between the floor and the ceiling, two lines of the same growth. At 20, 190 lies between 100 and 200.

    why it is allowed: By line 11, from 2 numbers on.

  13. the move: ceiling and floor together

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

    why it is allowed: Lines 9 and 12 bound WW by the same growth, n2n^2.

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

Why do 1 to nn, spread over the buckets in turn, cost the fewest looks, and how many?
  1. the move: a look is a pair

    the line:
    each pair sharing a bucketmakes exactly 1 look\begin{gathered} \text{each pair sharing a bucket} \\ \text{makes exactly 1 look} \end{gathered}
    0010712172333140slotkeyslanded where a key already was
    7, 12, 3 and 17 put in. Bucket 2 holds 7, 12 and 17, 3 pairs: 7 and 12, 7 and 17, 12 and 17. 12 makes 1 look and 17 makes 2. 3 shares its bucket with nothing.

    why it is allowed: By line 1 of the one-bucket question, a put compares its number with every number already in its bucket. So two numbers in one bucket meet once, when the later goes in. Two in different buckets never meet.

  2. the move: even out the sizes

    the line:
    swap a number in a bucketof size c for one landingin a bucket of size atmost c−2: a look saved\begin{gathered} \text{swap a number in a bucket} \\ \text{of size } c \text{ for one landing} \\ \text{in a bucket of size at} \\ \text{most } c - 2 \text{: a look saved} \end{gathered}
    1501107122233140slotkeyslanded where a key already was
    17 swapped for 15, which leaves 0. Bucket 2 drops to 7 and 12, 1 pair, and 15 shares bucket 0 with nothing.
    buckets 2 and 0 holdpairs in eachlooks
    3 and 03 and 03
    2 and 11 and 01
    Line 1's bucket 2 against bucket 2 after the swap. 17 leaves 2 pairs and joins none, so the swap saves 2 looks.

    why it is allowed: Swap 17 for 15. 17 met 7 and 12, so 2 pairs go; 15 lands in empty bucket 0, so none come: 2 looks saved, by line 1. In general, one fewer than cc pairs go and at most c−2c - 2 come.

  3. the move: the cheapest are even

    the line:
    the cheapest n numbers haveno two sizes 2 or more apart,as 1 to n do\begin{gathered} \text{the cheapest } n \text{ numbers have} \\ \text{no two sizes 2 or more apart,} \\ \text{as 1 to } n \text{ do} \end{gathered}
    001112712233832441slotkeyslanded where a key already was
    7 numbers with sizes 3, 2, 1, 1 and 0, largest first. 3 pairs in bucket 2 and 1 in bucket 3, 4 looks.
    50116122722331441slotkeyslanded where a key already was
    1 to 7, sizes 2, 2, 1, 1 and 1, largest first. 1 pair in bucket 1 and 1 in bucket 2, 2 looks, the fewest for 7 numbers.

    why it is allowed: If two sizes are 2 or more apart, line 2's swap saves a look, so that spread is not the cheapest. With no two sizes 2 or more apart, every size is qq or one more, where qq is nn over 5 rounded down. 5 buckets of qq hold 5q5q numbers, and the n−5qn - 5q left over make that many buckets one more. For 7, qq is 1, so 2 buckets hold 2 and 3 hold 1: 2 + 2 + 1 + 1 + 1 is 7. So all such spreads cost the same, by line 1, as 1 to 7 do.

  4. the move: price the kth put

    the line:
    spread in turn, the kth putmakes ⌊k−15⌋ looks\begin{gathered} \text{spread in turn, the } k\text{th put} \\ \text{makes } \lfloor \frac{k - 1}{5} \rfloor \text{ looks} \end{gathered}
    5100216111327122338324942slotkeyslanded where a key already was
    1 to 12 put in, in turn. 12 lands in bucket 2 and meets 2 and 7, 2 looks.

    why it is allowed: The k−1k - 1 numbers before it fill whole rounds of 5, one in each bucket, then part of a round. The part round fills only the buckets before its own in the turn 1, 2, 3, 4, 0. So its bucket holds one from each whole round. For 12, 11 over 5 is 2.2, rounded down 2: the 2 and the 7.

  5. the move: add the puts

    the line:
    B=⌊05⌋+⌊15⌋+⋯+⌊n−15⌋B = \lfloor \frac{0}{5} \rfloor + \lfloor \frac{1}{5} \rfloor + \dots + \lfloor \frac{n - 1}{5} \rfloor

    why it is allowed: BB is the looks of 1 to nn put in. By line 4, puts 1 to nn make ⌊05⌋\lfloor \frac{0}{5} \rfloor up to ⌊n−15⌋\lfloor \frac{n - 1}{5} \rfloor. For 10, five 0s and five 1s, 5.

  6. the move: count by rounds

    the line:
    B=5(0+1+⋯+(q−1))+ q (n−5q)\begin{gathered} B = 5\bigl(0 + 1 + \dots + (q - 1)\bigr) \\ + \, q\,(n - 5q) \end{gathered}
    putsroundlooks eachlooks
    1 to 5first00
    6 to 10second15
    11 and 12part of a third24
    in all9
    1 to 12 by rounds. Two whole rounds, so qq is 2, and 2 puts left over.

    why it is allowed: Let qq be the whole rounds of 5 puts, nn over 5 rounded down. In line 5, the rounds add 0, then 1, and on up to q−1q - 1, five times each. The n−5qn - 5q puts left over add qq each. For 12, qq is 2: 5 times 1, plus 2 times 2, is 9.

  7. the move: use the page on adding up

    the line:
    B=5(q−1)q2+q (n−5q)B = \frac{5(q - 1)q}{2} + q\,(n - 5q)
    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 q−1q - 1, and the 0 adds nothing: q−1q - 1 times qq, halved, five times. For 12, 5 times 1, plus 4, is 9.

  8. the move: drop the rounding

    the line:
    B≤0+1+⋯+(n−1)5B \le \frac{0 + 1 + \dots + (n - 1)}{5}

    why it is allowed: Line 7's qq is awkward beside n2n^2, so for how fast BB grows, go back to line 5. Rounding down never makes a number bigger. Fifths add their tops: 0 fifths, 1 fifth and 2 fifths make 3 fifths. For 10, 5 is at most 45 over 5, which is 9.

  9. the move: use the page on adding up

    the line:
    B≤(n−1)n10B \le \frac{(n - 1)n}{10}
    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 same box, with last number n−1n - 1: line 8's top is (n−1)n2\frac{(n - 1)n}{2}, and a fifth of that is (n−1)n10\frac{(n - 1)n}{10}. For 10, 9.

  10. the move: a ceiling

    the line:
    B≤n210B \le \frac{n^2}{10}

    why it is allowed: In line 9, n−1n - 1 is less than nn. For 10, 9 is at most 10.

  11. the move: read it as a bound

    the line:
    B∈O(n2)B \in O(n^2)

    why it is allowed: By line 10, with the fixed multiple a tenth.

  12. the move: rounding down loses at most 4 fifths

    the line:
    B≥0+1+⋯+(n−1)5−4n5B \ge \frac{0 + 1 + \dots + (n - 1)}{5} - \frac{4n}{5}
    toptop over 5rounded downlost
    5110
    61.210.2
    71.410.4
    81.610.6
    91.810.8
    Rounding a fifth down loses 0, 1, 2, 3 or 4 fifths, never more.

    why it is allowed: Each of line 5's nn pieces is its fifth, less what rounding down loses, at most 4 fifths. So nn pieces lose at most nn times 4 fifths. For 10, 5 is at least 9 less 8.

  13. the move: the same sum

    the line:
    B≥(n−1)n10−4n5B \ge \frac{(n - 1)n}{10} - \frac{4n}{5}

    why it is allowed: Line 12's sum is line 8's, which line 9 found to be (n−1)n10\frac{(n - 1)n}{10}. For 10, 9 less 8, which is 1.

  14. the move: tidy

    the line:
    B≥n(n−9)10B \ge \frac{n(n - 9)}{10}

    why it is allowed: In line 13, 4 fifths of nn is 8 tenths of nn. Take nn out of both tenths: nn times (n−1)(n - 1), less nn times 8, is nn times (n−9)(n - 9). For 20, 20 times 11 over 10 is 22, and BB is 30.

  15. the move: read it as a bound

    the line:
    B∈Ω(n2)B \in \Omega(n^2)
    looksnumbers put in050100150200010203040looksnumbers put in050100150200010203040
    • the ceiling, a tenth of n2n^2
    • BB, spread in turn
    • the floor, a twentieth of n2n^2
    From 18 numbers on, BB stays between a twentieth and a tenth of n2n^2. At 40, 140 lies between 80 and 160.

    why it is allowed: By line 14. From 18 numbers on, 9 is at most half of nn, so n−9n - 9 is at least half of nn. Then BB is at least a twentieth of n2n^2, the fixed multiple. At 20, 30 is at least 20.

  16. the move: ceiling and floor together

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

    why it is allowed: Lines 11 and 15 bound BB by the same growth, n2n^2.

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

Why do numbers landing at random cost (n−1)n10\frac{(n - 1)n}{10} looks on average?
  1. the move: average one put over its bucket

    the line:
    the kth put makes,on average, k−15looks: the load\begin{gathered} \text{the } k\text{th put makes,} \\ \text{on average, } \frac{k - 1}{5} \\ \text{looks: the load} \end{gathered}
    the third number lands innumbers therelooks
    bucket 0none0
    bucket 1none0
    bucket 2271
    bucket 3131
    bucket 4none0
    on average2 over 5
    13 and 27 are in. The third number's 5 buckets make 2 looks in all, 2 over 5 on average, numbers over buckets.

    why it is allowed: Its bucket is any of the 5, each as likely, so average its looks over the 5. By line 1 of the one-bucket question, each bucket's looks are the numbers there. Wherever the k−1k - 1 numbers already in went, the 5 buckets hold k−1k - 1 in all. Had 13 and 28 shared bucket 3, it would give 2 and the rest 0: still 2 over 5.

  2. the move: add the puts' averages

    the line:
    A=05+15+⋯+n−15A = \frac{0}{5} + \frac{1}{5} + \dots + \frac{n - 1}{5}

    why it is allowed: By line 1, whatever the earlier numbers did, puts 1 to nn average 0 up to n−1n - 1 over 5. Let AA be these averages added. For 3, 3 over 5.

  3. the move: an average adds its pieces

    the line:
    the average looks in all=05+15+⋯+n−15=A\begin{gathered} \text{the average looks in all} \\ = \frac{0}{5} + \frac{1}{5} + \dots + \frac{n - 1}{5} = A \end{gathered}
    buckets of 3 numbersput 2's looksput 3's lookslooks in all
    0, 0, 0123
    0, 0, 1101
    0, 1, 0011
    0, 1, 1011
    1, 0, 0011
    1, 0, 1011
    1, 1, 0101
    1, 1, 1123
    on average1 over 213 over 2
    3 numbers into 2 buckets, all 8 choices. Put 2 averages 1 over 2 and put 3 averages 1, the load before each. The totals average 3 over 2, their sum.
    where does this come from?
    question 2 of the page expected costShuffle nn different numbers at random. How many pairs stand out of order, on average?
    1. the move: out of order half the time

      the line:
      a pair stands out of orderwith chance one half\begin{gathered} \text{a pair stands out of order} \\ \text{with chance one half} \end{gathered}
      in order1231322133 shufflesout of order3212313123 shuffles=
      Trading places 1 and 3 pairs each shuffle holding them in order with one holding them out of order, 3 and 3.
      shuffleplaces 1 and 2places 1 and 3places 2 and 3pairs out of order
      1, 2, 30
      1, 3, 2yes1
      2, 1, 3yes1
      2, 3, 1yesyes2
      3, 1, 2yesyes2
      3, 2, 1yesyesyes3
      in all3339
      Each shuffle of 1, 2 and 3, a yes where a pair of places is out of order. 9 over 6 is 1.5.

      why it is allowed: A pair is two places. Trading their numbers pairs each shuffle with another, and trading back undoes it. Each two has the pair out of order in exactly one, and both are as likely: chance one half.

    2. the move: expected counts add

      the line:
      the expected countadds up the chanceof every pair\begin{gathered} \text{the expected count} \\ \text{adds up the chance} \\ \text{of every pair} \end{gathered}

      why it is allowed: The table at line 1 adds two ways: down its last column, 9, and along its bottom row, 3 + 3 + 3. Over 6 shuffles, 9 over 6 is the average count, 1.5, and 3 over 6 three times is the pairs' chances added, 1.5 too. Both add the same yeses, so they agree for any nn, though the pairs depend on each other. With unequal chances, weight each yes by its order's chance. 2, 1, 3 at 3 over 4 and 1, 2, 3 at 1 over 4 give 3 over 4 both ways.

    3. the move: one half for each pair

      the line:
      one half timesthe number of pairs\begin{gathered} \text{one half times} \\ \text{the number of pairs} \end{gathered}

      why it is allowed: By line 2 the expected count adds line 1's one half once per pair.

    4. the move: count the pairs

      the line:
      P=12×((n−1)+⋯+1)P = \frac{1}{2} \times \bigl((n - 1) + \dots + 1\bigr)

      why it is allowed: Let PP be the expected count, by line 3 half the number of pairs. The first place pairs with the n−1n - 1 after it, the next with one fewer, down to 1. For 3 places, 2 + 1 is 3 pairs, so 1.5.

    5. the move: add them the other way round

      the line:
      P=12×(1+⋯+(n−1))P = \frac{1}{2} \times \bigl(1 + \dots + (n - 1)\bigr)

      why it is allowed: The same numbers in the other order.

    6. the move: use the page on adding up

      the line:
      P=12×(n−1)((n−1)+1)2P = \frac{1}{2} \times \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2}
      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 page on adding up 1 to nn adds the numbers from 1 to any whole number. They add up to that number times one more than it, halved: 1 + 2 + 3 is 3 times 4, halved, which is 6. In its working, its sum runs from 1 up to its own last number, which it calls nn. Here that is n−1n - 1.

    7. the move: tidy the bracket

      the line:
      P=12×(n−1)n2P = \frac{1}{2} \times \frac{(n - 1)n}{2}

      why it is allowed: (n−1)+1(n - 1) + 1 is nn.

    8. the move: multiply the halves

      the line:
      P=(n−1)n4P = \frac{(n - 1)n}{4}

      why it is allowed: Half of a half is a quarter. For 3, 2 times 3 over 4 is 1.5.

    9. the move: read it

      the line:
      half the pairs,on average\begin{gathered} \text{half the pairs,} \\ \text{on average} \end{gathered}

      why it is allowed: By lines 4 to 7 there are (n−1)n2\frac{(n - 1)n}{2} pairs, and line 8 is half that. For 3, 1.5, as line 1 counted.

    the whole page: expected cost

    why it is allowed: The box under this line shows that an average count adds up each piece's chance, at its line 2. Count the kkth put's looks as one piece for each number already in, a yes when they share a bucket, with chance 1 over 5. Its pieces add to line 1's average, so AA is the looks averaged over every choice of buckets. The table shows it on 2 buckets, 8 rows, where 5 buckets would need 125.

  4. the move: take out the fifth

    the line:
    A=0+1+⋯+(n−1)5A = \frac{0 + 1 + \dots + (n - 1)}{5}

    why it is allowed: Fifths add their tops, as 0 fifths, 1 fifth and 2 fifths make 3 fifths.

  5. the move: use the page on adding up

    the line:
    A=(n−1)n10A = \frac{(n - 1)n}{10}
    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 n−1n - 1, so this question's line 4 has (n−1)n2\frac{(n - 1)n}{2} on top, and a fifth of that is (n−1)n10\frac{(n - 1)n}{10}. For 3, 2 times 3 over 10 is 0.6.

  6. the move: a ceiling

    the line:
    A≤n210A \le \frac{n^2}{10}

    why it is allowed: In line 5, n−1n - 1 is less than nn. For 3, 0.6 is at most 0.9.

  7. the move: read it as a bound

    the line:
    A∈O(n2)A \in O(n^2)

    why it is allowed: By line 6.

  8. the move: a floor

    the line:
    A≥n220 for n≥2A \ge \frac{n^2}{20} \text{ for } n \ge 2

    why it is allowed: In line 5, write half of nn in place of n−1n - 1, no bigger from 2 numbers on by line 10 of the one-bucket question. Half of nn times nn, over 10, is n220\frac{n^2}{20}. For 3, 0.6 is at least 0.45.

  9. the move: read it as a bound

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

    why it is allowed: By line 8, from 2 numbers on.

  10. the move: ceiling and floor together

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

    why it is allowed: Lines 7 and 9 bound AA by the same growth, n2n^2. At random costs little more than spread in turn, 990 looks against 950 at 100 numbers.

try it on small inputs: over every choice of bucket for each of 3 numbers, 125 in all, the average is A=0.6A = 0.6

Give the table as many buckets as numbers. How many looks do nn numbers landing at random cost then, on average?
  1. the move: the load with n buckets

    the line:
    with n buckets, the kth putmakes k−1n looks, on average\begin{gathered} \text{with } n \text{ buckets, the } k\text{th put} \\ \text{makes } \frac{k - 1}{n} \text{ looks, on average} \end{gathered}
    putnumbers already inlooks on average, 4 buckets
    100
    211 over 4
    322 over 4
    433 over 4
    in all6 over 4, which is 1.5
    4 numbers into 4 buckets. The load climbs to 3 over 4, never past 1, so no put averages a whole look.

    why it is allowed: Line 1 of the average question, with nn buckets in place of 5. The load before the kkth put is k−1k - 1 numbers over nn buckets.

  2. the move: add the puts

    the line:
    E=0+1+⋯+(n−1)nE = \frac{0 + 1 + \dots + (n - 1)}{n}

    why it is allowed: EE is the looks on average. By line 1, added over the puts, as lines 2 and 3 of the average question add them. For 4, 6 over 4.

  3. the move: use the page on adding up

    the line:
    E=(n−1)n2nE = \frac{(n - 1)n}{2n}
    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 gives 1 up to n−1n - 1 as (n−1)n2\frac{(n - 1)n}{2}, as line 5 of the average question uses it. Here it is divided by nn.

  4. the move: tidy

    the line:
    E=n−12E = \frac{n - 1}{2}

    why it is allowed: The nn on top and below cancel. For 4, 1.5 looks over 4 puts, under half a look each.

  5. the move: a ceiling

    the line:
    E≤n2E \le \frac{n}{2}

    why it is allowed: In line 4, n−1n - 1 is less than nn. For 4, 1.5 is at most 2.

  6. the move: read it as a bound

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

    why it is allowed: By line 5. Under half a look a put, however many numbers. A table that grows keeps its load at most a fixed number. So by line 1 each put averages at most that many looks: at most 0.75 in Java's (section 6).

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

count it yourself

Count every look as 8, 13, 18, 23 and 28 go into an empty table. All five leave 3.

8, 13, 18, 23, 28, every step written down
stepthe table, bucket: numberswhat happenscompared
1Start with 5 empty buckets, and 5 operations to do.0
28 leaves 3 when divided by 5, so it belongs in bucket 3.0
33: 88 is not in bucket 3 yet, so put it in.3: 80
43: 813 leaves 3 when divided by 5, so it belongs in bucket 3.3: 80
53: 88 is not 13.3: 81
63: 8, 1313 is not in bucket 3 yet, so put it in.3: 8, 131
73: 8, 1318 leaves 3 when divided by 5, so it belongs in bucket 3.3: 8, 131
83: 8, 138 is not 18.3: 8, 132
93: 8, 1313 is not 18.3: 8, 133
103: 8, 13, 1818 is not in bucket 3 yet, so put it in.3: 8, 13, 183
113: 8, 13, 1823 leaves 3 when divided by 5, so it belongs in bucket 3.3: 8, 13, 183
123: 8, 13, 188 is not 23.3: 8, 13, 184
133: 8, 13, 1813 is not 23.3: 8, 13, 185
143: 8, 13, 1818 is not 23.3: 8, 13, 186
153: 8, 13, 18, 2323 is not in bucket 3 yet, so put it in.3: 8, 13, 18, 236
163: 8, 13, 18, 2328 leaves 3 when divided by 5, so it belongs in bucket 3.3: 8, 13, 18, 236
173: 8, 13, 18, 238 is not 28.3: 8, 13, 18, 237
183: 8, 13, 18, 2313 is not 28.3: 8, 13, 18, 238
193: 8, 13, 18, 2318 is not 28.3: 8, 13, 18, 239
203: 8, 13, 18, 2323 is not 28.3: 8, 13, 18, 2310
213: 8, 13, 18, 23, 2828 is not in bucket 3 yet, so put it in.3: 8, 13, 18, 23, 2810
223: 8, 13, 18, 23, 28Done. The table holds 5 numbers.3: 8, 13, 18, 23, 2810

In all, 10 compared. The formula from section 4, (n−1)n2\frac{(n - 1)n}{2}, gives W=10W = 10 at n=5n = 5: the same.

where you meet it

Chaining is how Java's HashMap and C++'s unordered_map, the usual tables of two programming languages, are built, with more buckets added as the numbers come.

  • Java's HashMap

    Java's HashMap keeps a chain in each bucket, and turns one grown past 8 into a small tree once it has 64 buckets. It starts with 16 buckets and doubles them when the load passes 0.75. The table below follows it.

    source: OpenJDK's src/java.base/share/classes/java/util/HashMap.java, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, putVal and resize; Java's documentation, java.util.HashMap (opens in a new tab)

  • C++'s std::unordered_map

    C++'s unordered_map keeps its numbers in buckets and reports its load, which it tries to keep at most 1 unless told otherwise.

    source: the C++ standard, unord.req (opens in a new tab), the unordered containers' requirements, and unord.map.cnstr (opens in a new tab), its starting max_load_factor

numbers heldbucketsload
116about 0.06
12160.75
1332about 0.41
24320.75
2564about 0.39
A Java HashMap made empty, with no size given. Once the load passes 0.75, the buckets double and each number goes to its bucket among the new ones. A small whole number's bucket is its remainder. 17 leaves 1 divided by 16 and 17 divided by 32, so it moves from bucket 1 of 16 to bucket 17 of 32.

what is used instead

When two numbers want one place, dict, Python's own table, does not chain them. It uses open addressing: the second tries other places of the table itself, as the hash table with open addressing does. Sebastian Bourges' books say so, and still give the rule: 'Chaining is the answer you should have to be argued out of' (volume two, chapter 17).

how it compares

Two hash tables side by side, one with 5 buckets that never grow and one with as many buckets as numbers. Each puts nn different numbers in, one at a time.

wayn numbers at random, on averageextra memoryreach for it when
this table, 5 buckets that never grow(n−1)n10\frac{(n - 1)n}{10} looks5 buckets, each a list of its numbersthe numbers stay few: 10 numbers cost 9 looks
a table with as many buckets as numbersn−12\frac{n - 1}{2} looksa bucket for each number, each a listmany numbers: 1,000 cost about 500 looks, not 99,900
Both put each number in by walking one bucket. Section 4 works out both counts. Ten times the numbers costs the first table about a hundred times the looks, the second about ten times.
looks on averagenumbers put in050100150200010203040looks on averagenumbers put in050100150200010203040
  • 5 buckets, (n−1)n10\frac{(n - 1)n}{10}
  • as many buckets as numbers, n−12\frac{n - 1}{2}
The two tables part as the numbers grow. At 40 numbers, 156 looks against 19.5.

race them on one row of your own