Introduction
A hash function sends key to slot of a table , and collisions (two keys with the same slot) cannot be avoided once the universe is larger than the table (see Hashing and Hash Functions). The cost analysis uses expectation and linearity of expectation from Random Variables, Expectation and Variance. A collision-resolution method decides where the second key goes and how a later search finds it.
There are two families. Chaining keeps a linked list in each slot, so a slot can hold any number of keys. Open addressing stores every key in the table itself and, on a collision, probes other slots in a fixed order until it finds a free one. This note gives the algorithms, traces all four standard methods on one input, and states the expected number of probes as a function of the load factor .
Prerequisites
- Hashing and Hash Functions: slots , the division method, load factor, simple uniform hashing.
Core concepts
Chaining
Slot holds a pointer to a linked list of all keys with (NIL if there are none). New keys go at the head of their list.
Open addressing
Each slot holds one key, NIL (never used) or DELETED (used, then emptied). The hash function takes a second argument, the probe number :
is the probe sequence of . INSERT takes the first slot in the sequence that is NIL or DELETED; SEARCH follows the same sequence until it finds or a NIL slot. For INSERT to succeed whenever the table has room, the probe sequence must be a permutation of . Because all keys live in the table, and so .
Let (and , ) be ordinary hash functions into . For , is Euler’s totient: the number of integers with and . The three standard probe sequences are:
| Method | Probe | Distinct probe sequences | Weakness |
|---|---|---|---|
| Linear probing | Primary clustering: occupied runs grow and merge | ||
| Quadratic probing | , | Secondary clustering: keys with the same home share the whole sequence; may not reach every slot | |
| Double hashing | ; for prime or power-of-two | None of these; needs relatively prime to |
Primary versus secondary clustering. In linear probing, any key whose home slot falls anywhere inside an occupied run lands at the run's end and lengthens it, so long runs grow faster than short ones (primary clustering). In quadratic probing, only keys with the same home slot follow the same sequence (secondary clustering), which is milder. Double hashing gives keys with the same home different step sizes , so it avoids both.
Double hashing condition. The sequence visits every slot exactly when . Two standard ways: prime and , for example ; or a power of 2 and always odd.
Quadratic probing may not visit every slot. With and , the offsets for take only the 6 values . With an odd prime, reaches exactly residues, so an insertion can fail even though the table has free slots. It is guaranteed to succeed while the table is at most half full.
Algorithms and pseudocode
Pseudocode uses 0-based slots , <- for assignment, x.key for the key of element x, and NIL and DELETED as markers.
Chaining
CHAINED-HASH-INSERT(T, x)
1 insert x at the head of list T[h(x.key)]
CHAINED-HASH-SEARCH(T, k)
1 search list T[h(k)] for an element with key k
CHAINED-HASH-DELETE(T, x)
1 delete x from list T[h(x.key)]INSERT is worst case if it does not check for duplicates. DELETE is worst case if it is given a pointer to the element and the lists are doubly linked (a singly linked list must first find the predecessor). SEARCH costs the length of one list.
Open addressing
HASH-INSERT(T, k)
1 for i <- 0 to m - 1
2 j <- h(k, i)
3 if T[j] = NIL or T[j] = DELETED then
4 T[j] <- k
5 return j
6 error "hash table overflow"
HASH-SEARCH(T, k)
1 for i <- 0 to m - 1
2 j <- h(k, i)
3 if T[j] = NIL then
4 return NIL // k is not in the table
5 if T[j] = k then
6 return j
7 return NIL
HASH-DELETE(T, k)
1 j <- HASH-SEARCH(T, k)
2 if j != NIL then
3 T[j] <- DELETEDDeletion leaves a marker. HASH-DELETE cannot set the slot to NIL: a later key whose probe sequence passed through this slot would then be cut off, because SEARCH stops at NIL. DELETED means "keep probing" to SEARCH and "free" to INSERT. With many deletions, search times depend on the number of markers as well as on , which is why chaining is preferred when keys are deleted often.
Counting convention. A probe is one examination of a slot. A successful first-try insertion or search costs 1 probe. In chaining, the cost of a search is the number of list elements compared, plus the cost of computing .
Worked examples
Insert , in that order, into a table with and home slot . The home slots are , so the first six keys go straight home and only 44 and 20 collide.
| Method | Probes for 44 | Probes for 20 | Final table, slots 0 to 10 (dash = empty) | Total probes |
|---|---|---|---|---|
| Chaining (insert at head) | 1 list | 1 list | slot 0: 44 then 77; slot 9: 20 then 31 | not applicable |
| Linear, | 0, 1: 2 probes | 9, 10, 0, 1, 2: 5 probes | 77 44 20 - 26 93 17 - - 31 54 | 13 |
| Quadratic, | 0, 1: 2 probes | 9, 10, 2: 3 probes | 77 44 20 - 26 93 17 - - 31 54 | 11 |
| Double, | 0, 5, 10, 4, 9, 3: 6 probes | 9, 10, 0, 1: 4 probes | 77 20 - 44 26 93 17 - - 31 54 | 16 |
The details that decide exam answers:
- Linear. 20 starts at 9 (31), wraps from 10 (54) to 0 (77) and 1 (44), and stops at 2. The keys in slots 9, 10, 0, 1 and 2 now form one run of length 5 across the wrap-around.
- Quadratic. 20 probes , , . It skips slots 0 and 1, which is why it needs fewer probes than linear probing here.
- Double hashing. , so 44 probes ; , so 20 steps by 1 from slot 9. On a single small input double hashing can need more probes; its advantage is in the expected cost over many keys, not on every input.

Where does the next key go under linear probing?
After the linear-probing insertions the free slots are 3, 7 and 8. If the next key's home slot is uniformly random over :
- It lands in slot 3 if its home is 9, 10, 0, 1, 2 or 3: probability .
- It lands in slot 7 if its home is 4, 5, 6 or 7: probability .
- It lands in slot 8 only if its home is 8: probability .
The free slot just after the longest run is the most likely target, so the run grows further. This is primary clustering in numbers.
Which insertion orders give this table?
A table of size 10 with and linear probing holds 42, 23, 34, 52, 46, 33 in slots 2 to 7, in that order, after six insertions into an empty table. Work out what each key needed when it was inserted.
- 42, 23, 34 and 46 sit in their home slots (2, 3, 4, 6), so each only needed its home slot to be free.
- 52 has home 2 but sits in slot 5, so 42, 23 and 34 were all inserted before it (otherwise it would have stopped earlier); 46 does not affect it, since 52 stops at slot 5 either way.
- 33 has home 3 but sits in slot 7, so slots 3, 4, 5 and 6 were all occupied first: 33 came after 23, 34, 52 and 46, and so it was inserted last.
So 33 is last, 52 follows 42, 23 and 34 (in any of orders), and 46 can go into any of the 5 positions among those four keys: insertion orders produce the table. The order 46, 34, 42, 23, 52, 33 is one of them; 46, 42, 34, 52, 23, 33 is not (52 would take slot 3 before 23 arrived).
Expected bounds in this section assume insertion-only/no-tombstone occupancy models. With deletions, live load factor alone does not capture tombstone occupancy.
Mathematical explanation
Chaining under simple uniform hashing
Let . The expected length of each list is .
- Unsuccessful search: examines one whole list, so the expected number of elements compared is and the time is .
- Successful search (key chosen uniformly from the stored keys, insertion at the head): the expected number of elements compared is
because the keys examined before the target are those inserted after it into the same list, and each of the other keys is in the target's list with probability and inserted after it in half the cases.
If , then and every chaining operation takes expected time.
Open addressing under uniform hashing
Uniform hashing assumes each key's probe sequence is equally likely to be any of the permutations of . For , the expected number of probes is:
| Operation | Expected probes (bound under uniform hashing) | ||
|---|---|---|---|
| Unsuccessful search, and insertion | at most | 2 | 10 |
| Successful search | at most | 1.386 | 2.558 |
Why . The first probe always happens. A slot is occupied with probability about , so a second probe happens with probability at most , a third with probability at most , and so on: . Inserting a key costs the same as an unsuccessful search for it. A successful search for the key inserted -th repeats that insertion, which cost at most probes; averaging over and bounding the sum by an integral gives the logarithm.
Linear probing is worse. Linear probing cannot satisfy the uniform hashing assumption, because it has only probe sequences. Its standard analysis assumes instead that each key's home slot is uniformly random and independent of the others (simple uniform hashing of the home slot). Under that assumption, and because of primary clustering, the classical approximations for large are about probes for an unsuccessful search and for a successful one: 2.5 and 1.5 at , but 50.5 and 5.5 at . Double hashing behaves close to the uniform-hashing bounds.

Important properties and observations
| Property | Chaining | Open addressing |
|---|---|---|
| Load factor | any , may exceed 1 | ; performance collapses as |
| Extra space | one pointer per key plus heads | none beyond the table |
| Deletion | simple and exact | needs DELETED markers |
| Worst-case search | (one long list) | with tombstones; without them and with distinct-slot probing |
| Expected unsuccessful search | at most probes (uniform hashing) | |
| Cache behaviour | pointer chasing | linear probing scans adjacent slots |
Common misconceptions.
- "Quadratic probing always finds a free slot." It is guaranteed only while the table is at most half full (with prime); beyond that an insertion can fail with free slots left.
- "Deletion in open addressing empties the slot." It must mark it DELETED, or later searches fail.
- "Double hashing works with any ." must never be 0 and must be relatively prime to ; with the sequence never leaves the home slot.
- "The order of insertion does not matter." With open addressing the final table depends on the order; with chaining only the order inside each list does.
GATE examination perspective
- PROBE TRACES. Insert the keys in the given order, write the probe sequence of every colliding key, and remember to wrap with . Count probes using the question's convention (first probe counts 1 unless stated otherwise).
- WHICH SEQUENCE GIVES THIS TABLE. For a given final linear-probing table, check each candidate order: a key can sit away from its home only if every slot from its home up to its position (cyclically) was occupied when it was inserted.
- COUNTING ORDERS. To count the insertion orders that give a linear-probing table, turn each displaced key into "inserted after every key in the slots it passed over", then count the orders that satisfy those precedences (30 in the worked example).
- NEXT-SLOT PROBABILITY. Under linear probing, the probability that the next key lands in free slot is (number of home slots from which probing reaches ) divided by , as in the worked example.
- EXPECTED COST FORMULAS. Chaining ; open addressing at most unsuccessful and successful. Plug in exactly as .
- DELETION TRAPS. A search after a deletion continues past DELETED slots.
Summary
Sources
- T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009: Chapter 11, Section 11.2 (hash tables with chaining and their analysis) and Section 11.4 (open addressing: linear probing, quadratic probing, double hashing and the uniform-hashing analysis).