2. Introduction :
• Linear Search takes O(n) and binary search takes O(log n)
• Search operation to be performed in time proportion O(1)
2 solutions:
1st Solution :
3. • Wastage of storage space :
• Therefore store ID with last 2 digit and in same index
2nd solution:
Using hash table
Using hash function
4. Hash Tables :
• Is a data structure
• Keys are mapped to array positions
• Value stored in hash table can be found in O(1)
Ex:
Hash function h is used to calculate the index
at which the element with key k will be
stored
The process of mapping keys to appropriate
locations in hash table is called hashing
5. Each key from set K is mapped to
locations generated by hash
function
K2 and k6 point to same memory
location
K5 and k7 same
Known as collision – 2/more keys
map to the same memory location
6. Hash Functions :
• Is a mathematical formula, when applied to a key, produces an integer which
can be used as an index for the key in hash table
• 4 functions:
1) Division Method
2) Multiplication method
3) Mid-Square method
4) Folding method
• Uses numeric keys
• If alphanumeric – ASCII value can be used for transformation
7. 1) Division Method :
• Simple method
• Works very fast
• Care – to select suitable value for M (prefer prime number)
• X is an integer
• Divides x by m, and then uses the remainder obtained
• Hash function – h(x) = X Mod M
9. 2) Multiplication Method :
• Steps:
1) Choose A – 0<A<1
2) Multiply key K by A
3) Extract functional part kA
4) Multiply result of step3 by size of hash table
Ex:
10. 3) Mid Square Method :
• Works in 2 steps:
1) Square the value of the k^2
2) Extract the middle r digits of the result
• All digits of the key value contribute to result
• Result is not dominated(k) = s