20021519-050 Sec-B Hash
20021519-050 Sec-B Hash
Hashing Assignment
BS_CS 6th B
2
U N I V E R S I T Y O F G U J R A T
43 30 22 18 17 24 34 3 13 38
As the table size is not given so we choose according to the no of keys and our table size will
be 10.
Hash Functions:
(key mod 7)
1. Insert 43
• h1(43) = 43 mod 10 = 3
• h2(43) = 7 - (43 mod 7) = 1
• Index = (3 + 0 * 1) mod 10 = 3 (Slot 3 is empty, so we insert 43 here).
2. Insert 30
• h1(30) = 30 mod 10 = 0
• h2(30) = 7 - (30 mod 7) = 5
• Index = (0 + 0 * 5) mod 10 = 0 (Slot 0 is empty, so we insert 30 here).
3. Insert 22
• h1(22) = 22 mod 10 = 2
• h2(22) = 7 - (22 mod 7) = 6
• Index = (2 + 0 * 6) mod 10 = 2 (Slot 2 is empty, so we insert 22 here).
4. Insert 18
• h1(18) = 18 mod 10 = 8
• h2(18) = 7 - (18 mod 7) = 3
• Index = (8 + 0 * 3) mod 10 = 8 (Slot 8 is empty, so we insert 18 here).
5. Insert 17
• h1(17) = 17 mod 10 = 7
• h2(17) = 7 - (17 mod 7) = 4
3
• Index = (7 + 0 * 4) mod 10 =7 (Slot 7 is empty, so we insert 17 here).
• 5. Insert 24
• h1(24) = 24 mod 10 = 4
• h2(24) = 7 - (24 mod 7) = 4
• Index = (4 + 0 * 4) mod 10 = 4(Collision occurs).
• Index = (4 + 1 * 4) mod 10 =8
• Index = (4 + 2 * 4) mod 10 =2
• Index = (4 + 3 * 4) mod 10 =6 . (Slot 6 is empty, so we insert 24 here)
6. Insert 3
• h1(3) = 3 mod 10 = 3
• h2(3) = 7 - (3 mod 7) = 3
• Index = (3 + 0* 5) mod 10 =3 (Collision occurs).
• Continue this process until an empty slot is found.
• Index = (3 + 1* 3) mod 10 = 6.
• Index = (3 + 2* 3) mod 10 = 9(Slot 9 is empty, so we insert 3 here)
7. Insert 34
• h1(34) = 34 mod 10 = 4
• h2(34) = 7 - (34 mod 7) = 1
• Index = (4+ 0 * 1) mod 10 = 4
• Index = (4+ 1 * 1) mod 10 = 5(Slot 5 is empty, so we insert 34 here).
9. Insert 13
• h1(13) = 13 mod 10= 3
• h2(13) = 7 - (13 mod 7) = 1
• Index = (3 + 0 * 1) mod 10 = 3 (Collision at index 3, so we increment i)
• Continue this process until an empty slot is found.
• Index = (3 + 1 * 1) mod 10 = 4 (Slot 4 is empty, so we insert 13 here).
•
10. Insert 38
• h1(38) = 38 mod 10 = 8
• h2(38) = 7 - (38 mod 7) = 4
• Index = (8 + 0 * 4) mod 10 = 8(Collision occurs).
4
Hash Table:
Index Keys
0 30
1 38
2 22
3 43
4 13
5 34
6 24
7 17
8 18
9 3
Hash Function:
1. Insert 43
• h(43) = 43 mod 10 = 2 =3
• Index 3 is empty, so we insert 43 at slot 3
2. Insert 30
• h(30) = 30 mod 10 = 0
• Index 0 is empty, so we insert 30 at slot 0
3. Insert 22
• h(22) = 22 mod 10 = 2
• Index 2 is empty, so we insert 22 at slot 2.
4. Insert 18
• h(18) = 18 mod 10 = 8
• Index 8 is empty, so we insert 18 at slot 8.
5
5. Insert 41
• h(41) = 41 mod 10 = 1
• Index 1 is empty, so we insert 41 at slot 1.
6. Insert 2
• h(2) = 2 mod 10 = 2 (Collision at index 2)
• Linear probing: Increment the index by 1 until an empty slot is found.
• Index 3 is not empty (collision), so we increment the index by 1.
• Index 4 is empty, so we insert 2 at slot 4.
7. Insert 34
• h(34) = 34 mod 10 = 4(Collision at index 4)
• Index 5 is empty, so we insert 34 at slot 5.
8. Insert 20
• h(20) = 20 mod 10 = 0
• Index 0 is not empty (collision), so we increment the index by 1.
• Index 1 is not empty (collision), so we increment the index by 1.
• Index 2 is not empty (collision), so we increment the index by 1.
• Index 3 is not empty (collision), so we increment the index by 1.
• Index 4 is not empty (collision), so we increment the index by 1.
• Index 5 is not empty (collision), so we increment the index by 1.
• Index 6 is empty, so we insert 20 at slot 6.
•
9. Insert 13
• h(13) = 13 mod 10 = 6 (Collision at index 6)
• Linear probing: Increment the index by 1 until an empty slot is found.
• Index 7 is empty, so we insert 13 at slot 7.
10. Insert 38
• h(38) = 38 mod 10 = 8
• Index 8 is not empty (collision), so we increment the index by 1.
• Index 9 is empty, so we insert 38 at slot 9.
Hash Table:
Index Keys
0 30
1 41
2 22
3 43
4 2
6
5 34
6 20
7 13
8 18
9 38