Bird
0
0
DSA Cprogramming

Collision Handling Using Open Addressing Linear Probing in DSA C

Choose your learning style9 modes available
Mental Model
When two items want the same spot in a table, we look for the next empty spot in order until we find one.
Analogy: Imagine parking cars in a row of parking spots. If your favorite spot is taken, you move forward one spot at a time until you find an empty one.
Index: 0  1  2  3  4  5  6
Table: [ ] [ ] [ ] [ ] [ ] [ ] [ ]
          ↑
       hash(key) points here
Dry Run Walkthrough
Input: Insert keys 10, 20, 30 into a hash table of size 7 using linear probing with hash function key % 7
Goal: Show how collisions are handled by moving to the next empty slot
Step 1: Insert 10: compute hash 10 % 7 = 3, slot 3 is empty, place 10 there
Index: 0  1  2  3  4  5  6
Table: [ ] [ ] [ ] [10] [ ] [ ] [ ]
Why: First insertion, slot is free so we put 10 there
Step 2: Insert 20: compute hash 20 % 7 = 6, slot 6 is empty, place 20 there
Index: 0  1  2  3  4  5  6
Table: [ ] [ ] [ ] [10] [ ] [ ] [20]
Why: Slot 6 is free, so 20 goes there without collision
Step 3: Insert 30: compute hash 30 % 7 = 2, slot 2 is empty, place 30 there
Index: 0  1  2  3  4  5  6
Table: [ ] [ ] [30] [10] [ ] [ ] [20]
Why: Slot 2 is free, so 30 goes there without collision
Step 4: Insert 17: compute hash 17 % 7 = 3, slot 3 is occupied by 10, move to next slot 4 which is empty, place 17 there
Index: 0  1  2  3  4  5  6
Table: [ ] [ ] [30] [10] [17] [ ] [20]
Why: Collision at slot 3, so linear probing moves to next free slot 4
Result:
Index: 0  1  2  3  4  5  6
Table: [ ] [ ] [30] [10] [17] [ ] [20]
Annotated Code
DSA C
#include <stdio.h>
#define TABLE_SIZE 7

int hash(int key) {
    return key % TABLE_SIZE;
}

void insert(int table[], int key) {
    int index = hash(key);
    int start_index = index;
    while (table[index] != -1) {
        index = (index + 1) % TABLE_SIZE; // move to next slot
        if (index == start_index) {
            printf("Hash table is full, cannot insert %d\n", key);
            return;
        }
    }
    table[index] = key;
}

void print_table(int table[]) {
    printf("Hash Table:\n");
    for (int i = 0; i < TABLE_SIZE; i++) {
        if (table[i] == -1) {
            printf("[%d]: empty\n", i);
        } else {
            printf("[%d]: %d\n", i, table[i]);
        }
    }
}

int main() {
    int table[TABLE_SIZE];
    for (int i = 0; i < TABLE_SIZE; i++) {
        table[i] = -1; // initialize all slots as empty
    }

    insert(table, 10);
    insert(table, 20);
    insert(table, 30);
    insert(table, 17);

    print_table(table);
    return 0;
}
int index = hash(key);
compute initial slot using hash function
while (table[index] != -1) {
check if slot is occupied to handle collision
index = (index + 1) % TABLE_SIZE; // move to next slot
move to next slot in linear probing
if (index == start_index) {
detect full table to stop infinite loop
table[index] = key;
insert key into found empty slot
OutputSuccess
Hash Table: [0]: empty [1]: empty [2]: 30 [3]: 10 [4]: 17 [5]: empty [6]: 20
Complexity Analysis
Time: O(n) in worst case because we may check every slot if many collisions occur
Space: O(n) because we store n keys in the table array
vs Alternative: Compared to chaining which uses linked lists, linear probing uses only the array but can have clustering causing slower insert/search
Edge Cases
Inserting into a full table
The code detects full table and prints an error message without infinite loop
DSA C
if (index == start_index) {
Inserting first element
Inserted directly at hashed slot since it is empty
DSA C
while (table[index] != -1) {
When to Use This Pattern
When you see a hash table with collisions and no extra lists, think of linear probing to find next free slot by moving forward.
Common Mistakes
Mistake: Not wrapping around the table when reaching the end during probing
Fix: Use modulo operation to wrap index back to start: index = (index + 1) % TABLE_SIZE
Mistake: Not checking for full table causing infinite loop
Fix: Add a check to stop when index returns to start_index
Summary
It handles collisions by moving forward to find the next empty slot in the hash table.
Use it when you want a simple way to resolve collisions without extra memory for linked lists.
The key insight is to keep trying the next slot until you find an empty one or confirm the table is full.