Dsa Lecture 13 Hash Tables
Dsa Lecture 13 Hash Tables
0 1 2 3 4 5 6 7
What if we want to add the following names Amina, Chakubanga, Jamila, Peter?
Strategies for dealing with collisions
Buckets
One obvious option is to reserve a two-dimensional array from the start.
The disadvantage of this approach is that it has to reserve quite a bit more space than will be
eventually required, since it must take into account the likely maximal number of collisions.
Also, when searching for a particular key, it will be necessary to search the entire column
associated with its expected position, at least until an empty slot is reached (Linear search).
You can also sort the column (Binary Search).
0 1 2 3 4 5 6 7
Alex Jane Chacha Paul
30 35 18 29
Amina Jamila Chakubanga Peter
23 37 25 50
Strategies for dealing with collisions (cont)
Direct chaining
Use linked lists instead of the full array.
This approach does not reserve any space that will not be taken up, but has the disadvantage that in order to find
a particular item, lists will have to be traversed.
However, adding the hashing step still speeds up retrieval considerably.
the complexity class of all operations is constant, i.e. O(1)
For traversal, we need to sort the keys, which can be done in O(nlog2 n)
Hence, this method is better than the previous.
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7