Hashing
Hashing
Hashing is a technique used to map data of arbitrary size to fixed-size values, usually for the purpose of
fast data retrieval. The process involves using a hash function to convert an input (or 'key') into a hash
code (or 'hash value'), which typically is an integer. This hash code is then used as an index to store the
actual data in a hash table
Hash Function: A function that takes an input (or 'key') and returns a hash code. The goal of a
good hash function is to distribute hash codes uniformly across the hash table to minimize
collisions.
Hash Table: A data structure that stores key-value pairs. The hash code determines the index
in the hash table where the data associated with a given key is stored.
Collisions: Occur when two keys produce the same hash code. Handling collisions efficiently
is crucial for maintaining the performance of a hash table.
Collision Resolution Techniques:
1. Separate Chaining /Closed Addressing/open Hashing: Each index in the hash table
points to a linked list of entries that share the same hash code. This method allows
multiple entries to be stored at the same index.
2. Open Addressing/Closed Hashing: When a collision occurs, the algorithm probes the
hash table to find an empty slot. There are several probing methods:
o Linear Probing: Check the next slot sequentially until an empty slot is found.
o Quadratic Probing: Use a quadratic function to determine the next slot.
o Double Hashing: Use a second hash function to determine the probe sequence.
Hash Table
92
43
35
36
27
18
45
Load Factor:
Time Complexity:
o Average-case: O (1) for insertion, deletion, and search if the hash function
distributes keys uniformly and the load factor (ratio of number of entries to table
size) is low.
o Worst-case: O (n) in cases of many collisions or poor hash function performance.
Space Complexity: Dependent on the size of the hash table and any additional data
structures used for collision resolution (e.g., linked lists in chaining).