Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

University of Science and Technology Chittagong

Faculty of Science Engineering and Technology

Course Title: Data Structure

Course Code: CSE 211

Submitted To: Submitted By:

Nadia Mehzabin
Mr. Sukanta Paul(SIR)
ID: 17010115
Lecturer, Dept. of CSE
Batch: 29th
FSET USTC
Presentation Topic

HASHING
Hashing

Basically ,Hashing is a searching technique.

The definition of Hashing:


Hashing is the process of indexing and retrieving
element(data) in a data structure to provide faster
way of finding the element using the hash key.
Hash table:
A table where items(data) are stored in a sysmetic way
followed by hash key.
Hash value:
This is like index value or address of the item in the
Hash table.
Hash key:
This is the actual data item stored in Hash table.
Hash Value

Fig: Hash table,Hash key,Hash value


Hash function

To store data in a Hash table we must use some


methods.
These methods are known as hash function.
Definition of hash function:
The relation between Hash key and Hash value is
known as Hash function.
Hash function methods

Hash function follows following methods:

Division method:
If there are m slots available,then the key is mapped to given m
slots using following formula:
H(k)=k(mod m)
Or,H(k)=k(mod m)+1
Here,k(mod m) denotes the reminder when k is divided by m.the
second formula is used where we want the hash address to range
from 1 to m,rather than from 0 to m-1.
 
Hash function methods( Cont’d )

Midsquare method:
In this method the formula is H(k)=l
Where the key k is squared.
And l is obtained by deleting digits from both ends of the k2
Folding method:
In this method, the given key k is partitioned into subparts
k1,k2,k3,k4,kn
Each of which has the same length as the required
address.now add all these parts together and ignore the
carry:
H(k)=k1+k2+k3+……………+kn is the formula.
Problem

Consider the following 4 digits employee numbers :


9614,5882,6713,4409,1825
Find the 2 digit hash address of each number using
(a) The division method, with m=97;(b) the
midsquare method;(c)the folding method without
reversing; and(d)the folding method with reversing.
Solution

Here,m=97
K1=9614,k2=5882,k3=6713,k4=4409,k5=1825
Division method

H(ki)=ki(mod) where i=1,2,3,4……….


Or,H(ki)=ki(mod m)+1
H(k1)=11,H(k2)=62,H(k3)=20,H(K4)=44,H(K5)=79
In this case that the memory address begin with 01 rather then
00,we choose that the function,
H(ki)=ki(mod m)+1
Then,H(K1)=11+1=12
H(K2)=62+1+63
H(K3)=20+1=21
H(K4)=44+1=45
H(K5)=79+1=80
Midsquare method

The following calculation is performed:


H(ki) =l [where i=1,2,3……..]
K12=92428996,k22=34597924,k32=45064369,k42=194
39281,k52=3330625
Then,H(k1)=28,H(k2)=97,H(K3)=64,H(k4)=39,H(k5)
=30
Folding method

This method is:


H(ki)=k1+k2+K3+…….+k 4
[where i=1,2,3……..]
H(k1)=69+14=10,H(k2)=58+82=40,H(K3)=67+13=8
0,H(K4)=44+9=53,H(k5)=18+25=43
Alternatively,one may want to reverse the second
part before,adding,trhus production the following
hash address:
H(k1)=96+41=37,H(k2)=58+28=86,H(k3)=67+31=98
,H(k4)=44+90|=34,H(k)=18+52=70

You might also like