0% found this document useful (0 votes)
9 views23 pages

Hashing

The document provides an overview of hashing as a searching method, highlighting its efficiency with constant time complexity O(1). It explains the use of hash tables, hash functions, and various collision resolution techniques such as linear probing, quadratic probing, double hashing, and chaining. Additionally, it discusses the advantages and disadvantages of these techniques in handling collisions in hash tables.

Uploaded by

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

Hashing

The document provides an overview of hashing as a searching method, highlighting its efficiency with constant time complexity O(1). It explains the use of hash tables, hash functions, and various collision resolution techniques such as linear probing, quadratic probing, double hashing, and chaining. Additionally, it discusses the advantages and disadvantages of these techniques in handling collisions in hash tables.

Uploaded by

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

Data Structure and

Algorithms

HASHING

1
Searching Methods
 Linear Search
 Binary Search
 Hashing

2
Linear search

3
Binary Search

4
Hashing
 Hashing is quite fast in practice. Unlike other
searching techniques, hashing is extremely efficient.
 The time taken by it to perform the search does not
depend upon the total number of
elements.It completes the search with constant time
complexity O(1).

5
Hashing Technique
In hashing,
An array data structure called as Hash table is used to store
the data items.
Based on the hash key value, data items are inserted into the
hash table.
A hash table is a data structure which stores elements and
allow insertion, searching and deletion to be performed in
O(1) time.
In a hash table, a hash function is used to map keys into hash
key values in a table.
If ‘h’ is a hash function and ‘k’ is key then h(k)is called the
hash key and is the index at which a record with key ‘k’ is
placed. 6
Hashing

7
Hash Functions
 Hash function takes the data item as an input and returns a
small integer value as an output.
 The small integer value is called as a hash value.
 Hash value of the data item is then used as an index for
storing it into the hash table.
Types of Hash Functions
There are various types of hash functions available such as-
 Division Hash Function
 Folding Hash Function
 Mid Square Hash Function

8
Hashing Functions

9
Hashing Functions

10
11
Hasing Functions

12
Hashing Functions

13
Collision
 Since a hash function gets us a small number for a
key which is a big integer or string, there is
possibility that two keys result in same value.
 The situation where a newly inserted key maps
to an already occupied slot in hash table is
called collision and must be handled using some
collision handling technique.

14
Collision Resolution
Techniques

15
Open addressing( Closed
Hashing)
Linear Probing
In this, we can search the next empty location in the array by
looking into the next cell until we find an empty cell.
This technique is called linear probing.
Suppose a new record with key k is to be inserted or
searched in hash table, but that memory location with hash
address H(k) = h is already filled. Then this record will be
searched for at locations h, h+1, h+2, h+3 …..
Let us consider a simple hash function as “key mod 7” and
sequence of keys as 50, 700, 76, 85, 92, 73, 101.

16
Linear Probing

17
Linear Probing
Advantage-
It is easy to compute.

Disadvantage-
The main problem with linear probing is clustering.
Many consecutive elements form groups.
Then, it takes time to search an element or to find
an empty location.

18
Quadratic Probing
Consider a record with hash address H(k) = h. then
instead of searching the locations at h, h+1, h+2,….,
we search th location at addresses
h, h+1, h+4, h+9……..

19
Double Hashing
 Here a second hash function H’ is used for
collision resolution.
 Suppose a record with key ‘k’ has has hash
addresses
H(k) = h and H(k) = h’ then we search the locations
with addresses
H, h+h’, h+2h’, h+3h’…….

20
Chaining (Open Hashing)
To handle the collision,
This technique creates a linked list to the slot for
which collision occurs.
The new key is then inserted in the linked list.
These linked lists to the slots appear like chains.
That is why, this technique is called as separate
chaining.

21
Chaining
 Let us consider a simple hash function as “key mod 7” and sequence of keys
as 50, 700, 76, 85, 92, 73, 101.

22
Chaining
 Advantages:
1) Simple to implement.
2) Hash table never fills up, we can always add more elements to
chain.
3) It is mostly used when it is unknown how many and how frequently
keys may be inserted or deleted.
 Disadvantages:
1) Wastage of Space (Some Parts of hash table are never used)
2) If the chain becomes long, then search time can become O(n) in
worst case.
3) Uses extra space for links.

23

You might also like