We've seen searches that allow you to look through data in O(n) time, and
searches that allow you to look through data in O(logn) time, but imagine
a way to find exactly what you want in O(1) time. Think it's not possible?
Think again! Hash tables allow the storage and retrieval of data in an
average time of O(1).
At its most basic level, a hash table data structure is just an array. Data
is stored into this array at specific indices designated by a
hash function. A hash function is a mapping between the set of input
data and a set of integers.
With hash tables, there always exists the possibility that two data elements
will hash to the same integer value. When this happens, a collision
results (two data members try to occupy the same place in the hash table
array), and methods have been devised to deal with such situations. In this
guide, we will cover two methods, linear probing and separate chaining,
focusing on the latter.
Hashing has uses elsewhere besides in hash tables. Certain string matching
algorithms, for example Rabin-Karp, take advantage of hashing to do string
searching in linear time as opposed to the quadratic time of the normal
brute-force string searching algorithm.