Collision resolution: linear probing, quadratic probing, double hashing
Resolve collisions inside the table: linear probing, quadratic probing, double hashing.
Syllabus says: “Collision Resolution: Linear probing, Quadratic Probing, Double hashing”
On this page · 15 sections
LINEAR: +1, +2, +3… QUADRATIC: +1², +2², +3²… DOUBLE: + i × h₂(k). Linear → primary clustering; quadratic → secondary clustering.
Bus seat taken? Linear: try the next seat, then the next. Quadratic: jump 1, then 4, then 9 seats ahead. Double hashing: your jump size depends on your own ticket number.
Open addressing stores every key in the table itself; on a collision it probes h(k, i) = (h(k) + f(i)) mod m with f(i) = i (linear), i² (quadratic) or i·h₂(k) (double hashing).
Learning objectives
- Compute probe sequences for all three open-addressing methods.
- Explain primary and secondary clustering.
- Delete correctly using tombstones.
Definition
In open addressing every key is stored in the table itself. On a collision we try slots h(k, 0), h(k, 1), h(k, 2), … — the probe sequence — until a free slot is found.
Probe sequences
| Method | i-th probe | Problem |
|---|---|---|
| Linear probing | (h(k) + i) mod m | Primary clustering: long runs of occupied slots grow and slow everything down. |
| Quadratic probing | (h(k) + i²) mod m | Secondary clustering: keys with the same home slot follow the same sequence. May fail to find a free slot even when one exists (at most ⌈m/2⌉ distinct slots when m is prime). |
| Double hashing | (h(k) + i·h₂(k)) mod m | h₂(k) must never be 0 and should be relatively prime to m (choose m prime). A common choice: h₂(k) = R − (k mod R), R prime < m. |
Deletion and tombstones
We cannot simply empty a slot: a search for a key further along the same probe sequence would stop early at the empty slot and wrongly report “not found”. Instead the slot is marked DELETED (a tombstone). Searches skip tombstones; insertions may reuse the first tombstone they pass, after confirming the key is not already present further along.
Worked example
| Key | Home h(k) | Probes | Stored at |
|---|---|---|---|
| 72 | 6 | (6 + 0) mod 11 = 6 | 6 |
| 13 | 2 | (2 + 0) mod 11 = 2 | 2 |
| 6 | 6 | (6 + 0) mod 11 = 6 → (6 + 1) mod 11 = 7 | 7 |
| 5 | 5 | (5 + 0) mod 11 = 5 | 5 |
| 10 | 10 | (10 + 0) mod 11 = 10 | 10 |
| 36 | 3 | (3 + 0) mod 11 = 3 | 3 |
| 43 | 10 | (10 + 0) mod 11 = 10 → (10 + 1) mod 11 = 0 | 0 |
| 17 | 6 | (6 + 0) mod 11 = 6 → (6 + 1) mod 11 = 7 → (6 + 2) mod 11 = 8 | 8 |
Final table — 0: 43 | 1: — | 2: 13 | 3: 36 | 4: — | 5: 5 | 6: 72 | 7: 6 | 8: 17 | 9: — | 10: 10
Now watch it run
Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶. Keys: Space play/pause, ← → step.
Algorithm / pseudocode
- 1Operation(k)
- 2h ← hash(k)
- 3for i ← 0 to m − 1: s ← probe(h, i)
- 4if T[s] empty: (insert) store k at first tombstone seen, else at s
- 5if T[s] empty: (search/delete) report not found
- 6if T[s] = tombstone: remember it, continue
- 7if T[s] = k: (search) found
- 8if T[s] = k: (delete) T[s] ← tombstone
- 9if T[s] = k: (insert) duplicate — reject
- 10otherwise collision → next i
- 11no slot found → fail
- 12end
C implementation
hashing_open_addressing.ctested ✓ 246/246 cases/* Hashing with open addressing — KTU PCCST303, Module 4
* h(k) = k mod m; collision resolution: linear, quadratic or double hashing
* (h2(k) = R - k mod R). Deletion uses tombstones (DELETED markers).
*
* Input : m method R then a list of operations until EOF:
* I key (insert) S key (search) D key (delete)
* method: 1 = linear, 2 = quadratic, 3 = double hashing
* Output: the final table, one line "TABLE: ..." ('-' empty, 'X' deleted). */
#include <stdio.h>
#define MAXM 101
#define EMPTY -1
#define DELETED -2
int table[MAXM], m, method, R;
int probe(int key, int i) {
int h = key % m;
if (method == 1) return (h + i) % m;
if (method == 2) return (h + i * i) % m;
return (h + i * (R - key % R)) % m;
}
int search(int key) {
for (int i = 0; i < m; i++) {
int s = probe(key, i);
if (table[s] == EMPTY) return -1; /* never-used slot: stop */
if (table[s] == key) return s; /* skip DELETED: continue */
}
return -1;
}
int insert(int key) {
int firstDeleted = -1;
for (int i = 0; i < m; i++) {
int s = probe(key, i);
if (table[s] == key) return -2; /* duplicate */
if (table[s] == DELETED) { if (firstDeleted < 0) firstDeleted = s; continue; }
if (table[s] == EMPTY) {
int t = (firstDeleted >= 0) ? firstDeleted : s;
table[t] = key;
return t;
}
}
if (firstDeleted >= 0) { table[firstDeleted] = key; return firstDeleted; }
return -1; /* no slot */
}
int delete(int key) {
int s = search(key);
if (s >= 0) table[s] = DELETED;
return s;
}
int main(void) {
char op; int key;
if (scanf("%d %d %d", &m, &method, &R) != 3 || m < 1 || m > MAXM || method < 1 || method > 3 || (method == 3 && R < 1)) {
printf("Invalid header\n"); return 1;
}
for (int i = 0; i < m; i++) table[i] = EMPTY;
while (scanf(" %c %d", &op, &key) == 2) {
if (key < 0) { printf("Invalid key\n"); return 1; }
if (op == 'I') printf("INSERT %d -> %d\n", key, insert(key));
else if (op == 'S') printf("SEARCH %d -> %d\n", key, search(key));
else if (op == 'D') printf("DELETE %d -> %d\n", key, delete(key));
}
printf("TABLE:");
for (int i = 0; i < m; i++) {
if (table[i] == EMPTY) printf(" -");
else if (table[i] == DELETED) printf(" X");
else printf(" %d", table[i]);
}
printf("\n");
return 0;
}
Sample run · m = 11, linear probing · real output from gcc
Input (stdin)
11 1 7 I 72 I 13 I 6 I 5 I 10 I 36 I 43 I 17
Output (stdout)
INSERT 72 -> 6 INSERT 13 -> 2 INSERT 6 -> 7 INSERT 5 -> 5 INSERT 10 -> 10 INSERT 36 -> 3 INSERT 43 -> 0 INSERT 17 -> 8 TABLE: 43 - 13 36 - 5 72 6 17 - 10
Complexity
Average O(1) when α is kept low (≈ 0.5–0.7); worst case O(m) probes. Under the uniform hashing assumption an unsuccessful search takes at most 1/(1 − α) probes on average, so performance degrades quickly as α → 1. Open addressing requires α ≤ 1.
Edge cases
- Wrap-around: probing past index m − 1 continues at 0.
- Full table: insertion must fail gracefully after m probes.
- Quadratic probing with non-prime m (try m = 4 in the lab) can leave slots unreachable.
Common mistakes
- Quadratic probing: using (h + i)² instead of h + i².
- Double hashing: adding h₂(k) to the previous slot correctly, but forgetting mod m.
- Setting a deleted slot to empty.
Asked in KTU exams (2)
- November 2025 · Q16a6 markssolved ✓Given the values 72,13,6,5,10,36,43 and 17 and a hash table of size 11. Show the resultant table after inserting values in the given order by using hash function h(k)=k%11 with each of the following collision strategies. i) Linear Probing ii) Quadratic Probing iii) Separate Chaining
- May 2026 · Q16a6 markssolved ✓Given the values 45,36,19,58 and 10 and a hash table of size 13. Show the resultant table after inserting values in the given order by using hash function h(k)=k%13 with each of the following collision strategies. i) Linear Probing ii) Quadratic Probing iii) Double hashing with secondary hash function h'(k)=7-k%7
Quick check — can you get them all?
Practice
Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.
Exam-ready summary
- Linear (h+i), quadratic (h+i²), double (h+i·h₂) — all mod m.
- Clustering: primary (linear), secondary (quadratic), minimal (double).
- Delete with tombstones.
References
- Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — Ch. 7 (Sorting), Ch. 8 (Hashing) §8.2.4
- Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — Ch. 2, 6, 7, 8, 11 §11.4
Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.
Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)