7.1: Time complexity and common uses of hash tables
- Page ID
- 47916
Hash tables are often used to implement associative arrays, sets and caches. Like arrays, hash tables provide constant-time O(1) lookup on average, regardless of the number of items in the table. The (hopefully rare) worst-case lookup time in most hash table schemes is O(n).[1] Compared to other associative array data structures, hash tables are most useful when we need to store a large numbers of data records.
Hash tables may be used as in-memory data structures. Hash tables may also be adopted for use with persistent data structures; database indexes commonly use disk-based data structures based on hash tables.
Hash tables are also used to speed-up string searching in many implementations of data compression.
In computer chess, a hash table can be used to implement the transposition table.
Footnotes
- ↑ The simplest hash table schemes -- "open addressing with linear probing", "separate chaining with linked lists", etc. -- have O(n) lookup time in the worst case where (accidentally or maliciously) most keys "collide" -- most keys are hashed to one or a few buckets. Other hash table schemes -- "cuckoo hashing", "dynamic perfect hashing", etc. -- guarantee O(1) lookup time even in the worst case. When a new key is inserted, such schemes change their hash function whenever necessary to avoid collisions.