Search Menu

Contents

What is a Hash Table?

page 3 of 3

What is a Hash Table?

What is a Hash Table?

What is a Hash Table?

What is a Hash Table?

Figure %: After adding "Steve" to the table

And "Spark" which hashes to 6:

Figure %: After adding "Spark" to the table

Now we add "Notes" which hashes to 3, just like "Steve":

Figure %: Collision solved - "Notes" added to table

Once we have our hash table populated, a search follows the same steps as doing an insertion. We hash the data we're searching for, go to that place in the array, look down the list originating from that location, and see if what we're looking for is in the list. The number of steps is O(1) .

Separate chaining allows us to solve the problem of collision in a simple yet powerful manner. Of course, there are some drawbacks. Imagine the worst case scenario where through some fluke of bad luck and bad programming, every data element hashed to the same value. In that case, to do a lookup, we'd really be doing a straight linear search on a linked list, which means that our search operation is back to being O(n) . The worst case search time for a hash table is O(n) . However, the probability of that happening is so small that, while the worst case search time is O(n) , both the best and average cases are O(1) .