Hash Table Search
Idea
Hash Table Search maps a key via hash function to a slot—trading space for time, achieving near lookups.
哈希表查找演示
此处可扩展为哈希函数、冲突处理等动画。
- 哈希函数映射
- 冲突处理(开放定址/链地址)
- 查找过程演示
When to use
- Large data, very frequent lookups
- High-performance equality queries
Algorithm
- 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)
...
Complexity
- Ideal:
- Worst (heavy collisions):
- In practice, near with good hash & load factor
Exercises
Exercise 1
Hash-table search idea and common collision strategies?
参考答案
思路:哈希映射 + 冲突处理。
步骤:
- 哈希函数定位
- 冲突用开放定址、链地址等
答案:哈希定位;开放定址、链地址等。
Exercise 2
Average complexity? When does it degrade to ?
参考答案
思路:装填因子与冲突。
步骤:
- 平均
- 哈希差或负载过高导致大量冲突时,最坏
答案:平均 ;冲突严重时退化 。
Past exam
2020·408
哈希查找过程哪两步?冲突处理有哪些?
参考答案
思路:定位 + 处理。
步骤:
- 哈希函数定位
- 冲突时开放定址或链地址
答案:定位 + 冲突处理;开放定址、链地址等。