Hash Table Search

Idea

Hash Table Search maps a key via hash function to a slot—trading space for time, achieving near O(1)O(1) lookups.

Hash table search demo

This placeholder can be extended with animations of the hash function and of collision handling.

  • Hash function mapping
  • Collision handling (open addressing / chaining)
  • Search process demo

When to use

  • Large data, very frequent lookups
  • High-performance equality queries

Algorithm

  1. Design a hash function H(x)H(x) mapping key xx to a slot.
  2. If empty, not found.
  3. If occupied, compare key.
  4. On collision, resolve (open addressing, chaining, etc.).

Pseudo-code:

pos = H(x)
if table[pos] == null:
    return -1
elif table[pos].key == x:
    return pos
else:
    // collision handling (e.g., linear probing or chaining)
    ...

Constructing a Hash Function

  • Division method (modulo): H(key)=key mod pH(key) = key \bmod p, where pp is the largest prime not greater than the table length mm. It spreads the keys most evenly and is the most common choice.
  • Direct addressing: H(key)=a×key+bH(key) = a \times key + b, suitable when the keys are distributed continuously.
  • Digit analysis: take the few digits of the key that are distributed most evenly.
  • Mid-square method: take the middle digits of the square of the key.
  • Folding method: split the key into several parts and add them up.

Collision Handling

Open Addressing

On a collision, look for the next free slot inside the hash table.

  • Linear probing: Hi=(H(key)+i) mod mH_i = (H(key) + i) \bmod m, which easily produces clustering.
  • Quadratic probing: Hi=(H(key)+i2) mod mH_i = (H(key) + i^2) \bmod m, which avoids clustering.

Chaining

All elements that hash to the same position are hung on one linked list. A search first locates the list, then scans it sequentially.

#include <stdlib.h>

#define M 13  // table length (a prime number)

typedef struct Node {
    int key;
    struct Node *next;
} Node;

// Hash function: division method
int H(int key) {
    return key % M;
}

// Search with chaining
Node *HashSearch(Node *hashtable[], int key) {
    int pos = H(key);
    Node *p = hashtable[pos];
    while (p != NULL && p->key != key)
        p = p->next;          // sequential search along the list
    return p;                 // the node that was found, or NULL
}

// Insert with chaining
void HashInsert(Node *hashtable[], int key) {
    int pos = H(key);
    Node *s = (Node *)malloc(sizeof(Node));
    s->key = key;
    s->next = hashtable[pos];  // insert at the head of the list
    hashtable[pos] = s;
}

Load Factor and Performance

The load factor α=number of elementstable length\alpha = \frac{\text{number of elements}}{\text{table length}} directly affects the search performance.

  • The larger α\alpha is, the more collisions there are, and the lower the search efficiency.
  • Average search length for chaining: ASLsuccess≈1+α2ASL_{\text{success}} \approx 1 + \frac{\alpha}{2}
  • Average search length for linear probing: ASLsuccess≈12(1+11−α)ASL_{\text{success}} \approx \frac{1}{2}\left(1 + \frac{1}{1-\alpha}\right)
  • In engineering practice α\alpha is usually kept in the 0.7 to 0.8 range.

Complexity

  • Ideal: O(1)O(1)
  • Worst (heavy collisions): O(n)O(n)
  • In practice, near O(1)O(1) with good hash & load factor

Exercises

Exercise 1

Hash-table search idea and common collision strategies?

Hash TableHash FunctionCollision Handling

Approach: Hash mapping plus collision handling.

Steps:

  1. Locate the slot with the hash function
  2. Handle collisions with open addressing, chaining, etc.

Answer: Locate by hashing; handle collisions with open addressing, chaining, etc.

Exercise 2

Average complexity? When does it degrade to O(n)O(n)?

Hash TableLoad FactorTime Complexity

Approach: The load factor and the collisions.

Steps:

  1. On average O(1)O(1)
  2. When the hash function is poor or the load is too high, many collisions occur and the worst case is O(n)O(n)

Answer: On average O(1)O(1); it degrades to O(n)O(n) when collisions are heavy.

Past exam

2020·408

What are the two steps of the hash search process? Which collision-handling methods exist?

Hash TableCollision HandlingOpen Addressing

Approach: Locating the slot plus handling the collision.

Steps:

  1. Locate the slot with the hash function
  2. On a collision, use open addressing or chaining

Answer: Locating plus collision handling; open addressing, chaining, etc.

Found an issue with this content?

Tell us what could be improved.