Academic Session 2025-26
ODD Semester Jul-Dec 2025
UNIVERSITY INSTITUTE OF ENGINEERING
CSE-2nd year
CS201/IT201
SEMESTER 3
Advanced Data Structures and Algorithms
(24CSH-202)
Unit No. 2 Chapter No. 2.3 Topic : Hashing
____________________________________________________
Faculty Name and E_Code: Bhavneet Kaur(E14414)
Designation: Assistant Professor, Master Subject Co-ordinator
Learning Objectives
• Understand the Fundamentals of Hashing
• Analyse and Apply Collision Resolution Techniques
• Evaluate the Efficiency of Hashing Methods
Hashing
• Technique to perform Insertion, deletion &
search operations in the constant average
time by implementing Hash table Data
Structure .
• It is used to Index and Retrieve Items in a
Database.
• NEED:In Data Structures, Hashing is a
technique used to uniquely identify a
specific object from a group of similar
objects.
Real-Time Application
Password Storage in Websites Scenario:
When you create an account on a website, you provide a password. The website
should never store your actual password directly for security reasons.
How Hashing is Used:
•The password is passed through a hash function (like SHA-256).
•This function generates a hash value (a fixed-size string).
•This hash value is stored in the database — not the actual password.
•When you try to log in, the entered password is hashed again, and the hash is
compared with the stored one.
Why it's useful:
•Increases security — even if the database is hacked, the actual passwords
remain hidden.
•Hash functions are one-way — it's practically impossible to reverse-engineer
the password from the hash.
Types of Hashing
Dynamic
Static Hashing
Hashing
The Hash Table can
grow to handle
It is the hash function more Items.
maps search key
value toa fixed set of The associated Hash
locations. Function must
Change as the table
grows.
Hash Table
• The Hash Table data structure is a array
of some fixed size table containing the Keys.
• A Key is a values associated with each record.
• A Hash table is partition into array of size n.
• Each Bucket has many slots and each
slots holds one records.
Hash Function :
• A Hashing Function is a key to address
Transformation which acts upon a given Key to
complete the Relative Position of the Key in an
array.
• A Key can be a member of a String etc..
• A Hash Function Formula is
• Hash (Key Value ) =(Key Values % Table Size)
• Hash (Key Value)=Key Values % Table Size
• Examples,
• Hash(10) =10 % 5=0
• Hash(33) =33 % 5 =3
• Hash(11)=11 % 5=1
Hash(21)= 21 % 5=1
A Good Hashing consist of:
• Minimum Collision.
• Be easy and quick to complete.
• Distribute Key Value Every in the Hash Table.
• Use all the Information Provided in the Key.
Application of Hash Table:
• Database Systems
• Symbol Tables .
• Data Dictionaries
• Network Processing Algorithm
• Browse Casher.
Issue in Hashing
Imagine a university wants to store student records using a hash table.
Each student is identified by a roll number, and the hash function is: Hash Table (Size 10)
Index | Value
hash(roll_number) = roll_number % 10 -------------------------
Let’s insert these roll numbers into the hash table: 0 |
•Roll Number 101 → 101 % 10 = 1 → Stored at index 1 1 | 101 → 111 ← 🛑
•Roll Number 111 → 111 % 10 = 1 → Also wants to go to index 1 2 |
3 |
⚠️Collision Occurs: 4 |
Collision
Both roll numbers 101 and 111 hash to the same index 1. This 5 | handled
situation is called a collision — two different keys mapping to 6 | with
the same hash value. 7 | chaining
8 |
Collision Resolution: 9 |
To handle this:
•Chaining: Store both values in a linked list at index 1.
•Open Addressing: Find another available index using a probing method (like linear
probing, quadratic probing).
Collision
• Collision occurs when a hash values of a records being
Inserted hashes to an Address That already contains a
difference Record.
•When Two Key values hash to the position.
•Collision Resolution :
• The Process of finding another Position for The Collide Record Is
said to be collision Resolution Strategy.
•Two categories of Hashing .
1. Open Hashing
• Separate Chaining.
2. Closed Hashing .
• Open Addressing ,Rehashing
Collision Types
Open Hashing :
• Each Bucket in the Hash table is the head of a Linked List.
• All Elements that hash to a Particular Bucket are Placed on the
Buckets Linked List .
Closed Hashing:
• Ensures that all elements are stored directly in to the Hash Table.
Separate Chaining :
• Separate Chaining is an open hashing Technique
• A Pointer fields is added to each record Location.
• In this method the table can never overflow since the linked are
only extended upon or New Keys
•Separate Chaining
Example:
10 , 11 , 81 , 10 , 7 ,
34 , 94 , 17 , 29 , 89
, 99
•The element 81 collides to the same of the hash value to place
the value 81 at this position perform the following.
1. Traverse the list to check whether it is already present.
2. Since, it is not already present, insert at end of the list similarly, the
rest of the elements are inserted.
•Advantages:
More number of elements can be inserted as it uses of linked list.
Collision resolution is simple and efficient.
•Disadvantages:
•It requires pointers, which occupies more memory
space.
Closed hashing:
• Collide elements are stored at another slot in the table.
Ensures that all elements stored directly in the hash table.
•Eg: Open addressing
• Rehashing and extendable hashing.
Open Addressing:
• Open addressing also called closed hashing
which is an attentive to resolve the collision with
linked list.
• If a collision occurs, alternative cells are tried
until an empty cell is found (i.e) cells ho(x), h1(x),
h2(x) are tried in succession.
There are three common collision strategies,
there are
1. Linear Probing
2. Quadratic Probing
3. Double Hashing
Index |
Linear Probing (Open Values
Addressing) ---------------
0 | 105
Example: Storing book IDs in a 1 | 110
library system 2 |
3 |
•Hash Function: book_id % 5
4 |
•Insert: 105, 110
index=(h(key)+i)modTable Size
•Both hash to index 0 → Second •h(key) = Primary hash function
•i = Attempt number (starts from 0,
value looks for next empty slot
then 1, 2, 3, ...)
•Table Size = Size of the hash table
Quadratic Probing
Index | Values
Example: Parking slot ---------------
0 | 1205
assignment using car 1 |
registration numbers 2 | 1210 ← (0 + 2²) % 5 = 4 → tries 4 → empty → inserted
3 |
4 |
•Hash Function:
car_number % 5
•Probe next index using formula:
•Insert: 1205, 1210
•Both hash to index 0 new_index = (hash + i^2) %
size, for i = 1, 2, ...
Rehashing:
• If the table get’s to full. Then the rehashing
method new tables that is about twice as with and
scan down the entire original hash table.
• The entire original hash table, computing the
new hash value for each element and in sorting it in
the new table.
• Rehashing is very expensive operations, the
running time is O(N), Rehashing can be implements
is several ways with Quadratic probing such as
i). Rehash as soon as the table is half full.
Also known as Double Hashing:when a collision
occurs, a second hash function is used to calculate
the step size for probing. 6
Index | Value
Example, Storing Employee ---------------
IDs in a system 0 |
Let: 1 | 50
•Table size = 7 2 | 64
3 |
•Primary Hash Function: 4 | 57
h₁(key) = key % 7 5 |
•Secondary Hash Function: 6 |
h₂(key) = 5 - (key % 5)
Now insert: key = 50, 57, 64 Index=(h1(key)+i×h2
(key))modTable Size
•h₁(key) → Primary hash function
•h₂(key) → Secondary hash function
(must never be zero)
•i → Number of attempts (0, 1, 2, ...)
Advantages &Disadvantages
Technique Advantages Disadvantages
- Extra memory for pointers- Poor
- Simple to implement- Can handle
Chaining cache performance- Slower if many
high load factors- Easy deletion
collisions
- Primary clustering- Poor
- Simple- No extra memory- Good
Linear Probing performance when table is near full-
cache performance
Complex deletion
- Secondary clustering- Cannot
- Reduces clustering- Better than
Quadratic Probing guarantee finding a slot without a
linear in high load cases
good table size
- Minimizes both primary and - Needs two good hash functions-
Double Hashing secondary clustering- Best More complex- Slower due to extra
distribution hashing
Case Study- Playlist Management in a
Music Streaming App
• A social media platform needed a fast way to authenticate millions of users daily. The
system adopted hashing to securely store and access user credentials and profile data.
• Key Concepts Applied : Hash Tables for constant time lookup
- Hash Functions for key distribution
- Collision Resolution: Separate Chaining & Open Addressing
- Cryptographic Hashing (SHA-256) for passwords
• Reference: CLRS (2009), Chapter 11: Hash Tables
- Stallings, W. (2017), Cryptography and Network Security
- Real-world model: Facebook Engineering - Account Security Systems
Quiz
1.With respect to hash functions, what is meant by a collision? 3. Which of the following pairs of keys hash to the
Options: same location if a hash table of size 5 is used and the
a. Two different messages M1 and M2 may generate the same hash function is h(x) = x mod 5?
hash value H. Options:
b. The same message M may generate two different hash a. 11 and 15
values H1 and H2. b. 12 and 37
c. Two different hash functions produce the same hash value c. 16 and 42
for the same input. d. 25 and 89
d. A hash function maps every input to a unique hash value
2. Let the keys 75, 12, 8, 62, 83, 91, 15 be hashed to a hash 4.There is a hash table with 20 slots. 100 elements are
table of size 10 using the hash function h(x) = x mod 10. How stored onto this hash table. What is its load factor?
many collisions occur during the hashing process? Options:
Options: a. 120
a. 0 b. 80
b. 1 c. 2000
c. 2 d. 5
d. 3
5. If chaining is used as a collision resolution technique 6. If the keys 25, 17, 82, 65, 18, 9, 120 are hashed in the
for the above question, what is the average chain length? given order onto a hash table of size 7 with linear
probing as a collision resolution technique and the hash
Options:
function is h(x) = x mod 7, what will be the final contents
a. 1 of the hash table (in increasing order of indices)?
b. 1.5 Options:
c. 2 a. 9, 17, 25, 18, 82, 65, 120
d. 3 b. 9, 65, 25, 82, 18, 120, 17
c. 17, 65, 9, 18, 82, 25, 120
d. 9, 120, 65, 17, 25, 82, 18
~
•Week 9 Assignment 9 – Introduction to HashingNPTEL+19NPTEL+19NPTEL+19
•Week 8 Assignment – Hash Tables & Graph AlgorithmsNPTEL+3NPTEL+3NPTEL+3
•NOC: Programming, Data Structures and Algorithms Using Python
References
• https://www.google.co.in/books/edition/Mastering_Algorithms_with_C/PL1vI3p
bW6cC?hl=en&gbpv=1
Relevant learning resources
https://www.coursera.org/in/articles/hashing-in-data-struct
ure
https://www.linkedin.com/advice/3/what-some-applications-challen
ges-using-hash-tables
12
Thank You