Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 4 · syllabusAsked 2× in KTU papersChecked by code · teacher review pending

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
ormakkurippu ✎
Never-forget card

LINEAR: +1, +2, +3… QUADRATIC: +1², +2², +3²… DOUBLE: + i × h₂(k). Linear → primary clustering; quadratic → secondary clustering.

Picture it

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.

Write this in the exam

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

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

Methodi-th probeProblem
Linear probing(h(k) + i) mod mPrimary clustering: long runs of occupied slots grow and slow everything down.
Quadratic probing(h(k) + i²) mod mSecondary 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 mh₂(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

Worked example (KTU Nov 2025 Q16a, linear probing)checked by code
KeyHome h(k)ProbesStored at
726(6 + 0) mod 11 = 66
132(2 + 0) mod 11 = 22
66(6 + 0) mod 11 = 6 → (6 + 1) mod 11 = 77
55(5 + 0) mod 11 = 55
1010(10 + 0) mod 11 = 1010
363(3 + 0) mod 11 = 33
4310(10 + 0) mod 11 = 10 → (10 + 1) mod 11 = 00
176(6 + 0) mod 11 = 6 → (6 + 1) mod 11 = 7 → (6 + 2) mod 11 = 88

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 ◀ ▶.

Loading visualizer…

Algorithm / pseudocode

Open addressing (insert)
  1. 1Operation(k)
  2. 2h ← hash(k)
  3. 3for i ← 0 to m − 1: s ← probe(h, i)
  4. 4if T[s] empty: (insert) store k at first tombstone seen, else at s
  5. 5if T[s] empty: (search/delete) report not found
  6. 6if T[s] = tombstone: remember it, continue
  7. 7if T[s] = k: (search) found
  8. 8if T[s] = k: (delete) T[s] ← tombstone
  9. 9if T[s] = k: (insert) duplicate — reject
  10. 10otherwise collision → next i
  11. 11no slot found → fail
  12. 12end

C implementation

hashing_open_addressing.ctested ✓ 246/246 cases
C
/* 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

Open in the Code Lab →

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)

Quick check — can you get them all?

Question 1

Q1.m = 11, linear probing, table has 72 at index 6. Insert 6 (h = 6) goes to…

Question 2

Q2.Which method suffers from primary clustering?

Question 3

Q3.Why mark deleted slots with a tombstone instead of emptying them?

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)