algo·visual

Hash Table

Data Structures
avgO(1)worstO(n)spaceO(n + m)
h(k) = k mod 7
0
1
22
2
3
10
31
4
5
6

7 buckets holding 3 keys (load factor 0.43)

1 / 42
Starting contents3 values
Operations8 / 40
  1. 1. insert(15)
  2. 2. insert(29)
  3. 3. insert(8)
  4. 4. insert(22)
  5. 5. search(29)
  6. 6. delete(15)
  7. 7. search(44)
  8. 8. insert(36)

How it works

Maps each key to one of m buckets with hash(k) = k mod m and keeps colliding keys in a chain. With a good hash and a low load factor every chain is short, so insert, search and delete take O(1) on average. If every key collides, they take O(n).

Complexity

Best
O(1)
Average
O(1)
Worst
O(n)
Space
O(n + m)

Search algorithms

Jump to any algorithm or page