Professional Documents
Culture Documents
02 Hash Tables
02 Hash Tables
Chapter 2
Hash Tables
In a hash table, data is stored in an array format, where each data value has its own unique
index value.
Access of data becomes very fast if we know the index of the desired data.
Hash Table uses an array as a storage medium and uses hashing technique to generate an index
where an element is to be inserted or is to be located from.
Bucket Array
Department of Electronics And Electrical Engineering, IIT Guwahati
Hashing Algorithms
• Hashing is a technique to convert a range of key values into a range of indices of an array
• This array is called the bucket array
• Keys must be unique
Bucket Array
91 mod 10 = 1 3 83
67 mod 10 = 7 4 24
48 mod 10 = 8 7 67
83 mod 10 = 3 8 83
91 mod 6 = 1 2 68
68 mod 6 = 2 3 45
45 mod 6 = 3 4 52
83 mod 6 = 5 5 83
242=576→7 0 20
152=225→2 2 15
122=144→4 4 12
172=289→8 6 13
202=400→0 7 24
132=169→6 8 17
44 2 3 27
23 5 4
20 2 5 23
Department of Electronics And Electrical Engineering, IIT Guwahati
Linear Probing
searches for the next
available slot by probing
sequentially until an empty
slot is found
Disadvantages:
• Worst case search O(n)
• Linear probing may lead to clustering, where consecutive slots
are filled.
“key mod 7” and sequence of keys as 50, 700, 76, 85, 92, 73, 101
Double hashing uses the idea of applying a second hash function to key
when a collision occurs
The advantage of double hashing is that it is one of the best form of probing,
producing a uniform distribution of records throughout a hash table.
𝑅 ( 𝑘 ,𝑖 )=( 𝑅1 ( 𝑘 ) + 𝑖 𝑅 2 ( 𝑘 ) ) mod 𝑛
Table size
Prime number<n
Locate the element using that hash code as index in the array
Use collision handling to get the element ahead if the element is not found
at the computed hash code
Department of Electronics And Electrical Engineering, IIT Guwahati
Insert Operation
Whenever an element is to be inserted
Locate the index using that hash code as an index in the array.
Use collision handling for empty location, if an element is found at the computed hash code.
Locate the index using that hash code as an index in the array.
Use linear probing to get the element ahead if an element is not found at the computed hash code.