In: Computer Science
What is modulo and how is it used in a hashing function? Explain and give an example.
What is a collision in a hash table? Name and explain at least two ways in which a collision can be handled. Give examples.
What is modulo and how is it used in a hashing function? Explain and give an example.
Modulo in hash function can be defines as mod
The Equation can be given as h(k) = k mod N
Where k = hash code(integer) which is generated from the key.
N = number of buckets.
Example: h(k) = k mod 4
Key | Element |
0 | 45 |
1 | 67 |
2 | 78 |
3 | 12 |
What is a collision in a hash table? Name and explain at least two ways in which a collision can be handled. Give examples.
Collision in hash table:
If the Hash Function is mapped to two keys which are different in same table or if a key which is inserted newly is mapped to occupied slot in this two cases collision occurs.
Handling Collision:
(1).Collision can be avoided by selecting Hash function Properly.
(2).Collision can be handles by Open Addressing and Seperate chaining method.
Example:
Using seeprate chaining