You are on page 1of 20

This Photo by Unknown Author is licensed under CC BY-SA-NC

HASH TABLE
SABA ZAIRA 3908-FBAS/BSCS/F18
FATIMA RAUF 3907-FBAS/BSCS/F18
MUBARRA KHALID 3906-FBAS/BSCS/F18
ANEETA TABASUM 3893-FBAS/BSCS/F18
HASH TABLE
• Data Structure
• Key-Value pair
 unique keys
 Keys are address
 Each value is associated with mapped key
STRUCTURE

• Key is a value to insert


• Index is address of values
• After applying hash function to a key, we got an index
to place value
HASH FUNCTION
insert(76)
For NUMERICS: 76%7 = 6

• Calculation applies to large number to make it small and 0

then corresponds it to position in hash table. 1


• Divide key by size of hash table and reminder will be the 2
index .
3
FOR ALPHA NUMERIC:
4
• Divide sum of ASCII codes of key by size of hash table and
reminder will be the index . 5

6
76
FOR LARGE NUMBERS(FOLDING METHOD):
Break it into groups of 2 digits and add them then divide it to the size of hash
table and reminder will be the index.

NUM:
0145283450
01+45+28+34+50=203
203%size of hash table=address for num
COLLISION
When two keys have the same hash value
• example:
“9" and “620“ for size “13”
9%13=9
620%13=9
Here both the values collided.
IMPROVEMENTS:
• Linear probing
• Chaining
• Resizing the hash table
Linear
Probing
EXAMPLE
CHAINING
EXAMPLE
WORKING
INSERT
DELETE
FIND
COMPLAXIT
IES
Algorithm Best Average Worst case
Space O(n) O(n) O(n)
Search O(1) O(1) O(n)
Insert O(1) O(1) O(n)
Delete O(1) O(1) O(n)
Worst Case
If the keys we encounter,
all keys may hash to the
same bucket.

worst-case complexity of a hash table (same as that of a linked list): 


O(n) for insert, lookup and remove.
EXAMPLE
CODE

You might also like