Hash Table Search
Hash Table Search maps a key via hash function to a slot—trading space for time, achieving near 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
- Large data, very frequent lookups
- High-performance equality queries
- Design a hash function mapping key to a slot.
- If empty, not found.
- If occupied, compare key.
- 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)
...
- Division method (modulo): , where is the largest prime not greater than the table length . It spreads the keys most evenly and is the most common choice.
- Direct addressing: , 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.
Open Addressing
On a collision, look for the next free slot inside the hash table.
- Linear probing: , which easily produces clustering.
- Quadratic probing: , 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;
}
The load factor directly affects the search performance.
- The larger is, the more collisions there are, and the lower the search efficiency.
- Average search length for chaining:
- Average search length for linear probing:
- In engineering practice is usually kept in the 0.7 to 0.8 range.
- Ideal:
- Worst (heavy collisions):
- In practice, near with good hash & load factor
Exercise 1
Hash-table search idea and common collision strategies?
Approach: Hash mapping plus collision handling.
Steps:
- Locate the slot with the hash function
- 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 ?
Approach: The load factor and the collisions.
Steps:
- On average
- When the hash function is poor or the load is too high, many collisions occur and the worst case is
Answer: On average ; it degrades to when collisions are heavy.
2020·408
What are the two steps of the hash search process? Which collision-handling methods exist?
Approach: Locating the slot plus handling the collision.
Steps:
- Locate the slot with the hash function
- On a collision, use open addressing or chaining
Answer: Locating plus collision handling; open addressing, chaining, etc.
