0% found this document useful (0 votes)
18 views7 pages

20021519-050 Sec-B Hash

Uploaded by

ahmedch82281
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

20021519-050 Sec-B Hash

Uploaded by

ahmedch82281
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Design and Analysis of Algorithm

Course Code: CS-369

Hashing Assignment

Submitted to: Dr. Abdul Rehman

Submitted by: Muhammad Ahmed Raza

Roll No: 20021519-050

Submission Date: 221h September 2023

BS_CS 6th B

2
U N I V E R S I T Y O F G U J R A T

Using Double Hashing Technique:


Keys:

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:

h1(key) = key mod 10 h2(key) = 7 -

(key mod 7)

Inserting values into table:

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).

• Index = (8 + 1* 4) mod 10 = 2(Collision occurs).

• Index = (8 + 2* 4) mod 10 = 6(Collision)

• Index = (8 + 3* 4) mod 10 = 0(Collision)

• Index = (8 + 4* 4) mod 10 = 4 (Collision)

• Index = (8 + 5* 4) mod 10 = 8(Slot 1 is empty, so we insert 38 here).

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

Using Linear Prob:


Choosing a hash table size according to the no of keys and it is 10.

Hash Function:

h(key) = key mod 10

Inserting keys into Hash Table:

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy