CIS 22 - Data Structures: Hash Table
Introduction
Is it possible to design a search of O(1)– that is, one that has a constant search time, no matter where the element is located in the list?
In theory, this goal is not an impossible dream. Let’s look at an example. We have a list of employees of a fairly small company. Each of 100
employees has an ID number in the range 0 – 99. If we store the elements (employee records) in the array, then each employee’s ID number will be an
index to the array element where this employee’s record will be stored.


But what if we keep the array size down to the size that we will actually be using (100 elements) and use just the last two digits of key to identify each employee? For example, the employee with the key number 54876 will be stored in the element of the array with index 76. And the employee with the key 98759 will be stored in the element of the array with index 59. Note that the elements are not stored according to the value of the key as they were in the previous example. In fact, the record of the employee with the key number 54876 precedes the record of the employee with key number 98759, even though the value of its key is larger. Moreover, we need a way to convert a five-digit key number to two-digit array index. We need some function that will do the transformation. Using this technique we call the array a Hash Table and a function is called a Hash Function.