Hashing Technique
Hashing Technique
CH#QUE
?
whytnashing
•
.
,
order have do
Hashing
3 .
-
Oct ) so we to some entree work
,
It is the
fastest method ,
but
requires very large space
n
as it depends on
manimum element
\ DRAWBACK
I
A.E.li#Et
1-
÷tE%
Ideating
•
*¥i¥÷f÷¥t¥
hln) him )=n%
Mapping
=N to remainder
fun
'
✓
many one
-
-
One to one
-
°
8
one to °
many
#
-
'
3
Uman fun
'
to one I
y
-
fo I } many to
many
83 2
10 10
:
9
"
✓
Y,
collision
14
IS IS
#
•
HowtoavoidCOLLISION.ly
11
Opening NO
space restriction
Chaining
•
Quadratic Probing
2.
[
Double methods for free
Hashing storing in
3 .
space
•
CHAINING
#
o l
11 sorted order
16
,
T Arrange in
2
25
39 3 I
68
75
646
#
7
8
_¥
-
1
¥
-
first digit
Averagesuccessfulseaochh
basis of
Arrange on
In ) =
MY . to ← Hash Function t =
It I
2
-
Averagevnsuccessfulseaochlt-s.IE#n-oiayqor-/
n = 100 No of keys
size = 10 NO of index
t -
- it x
Ifke : 5 ,
35 ,
95 , 145 ,
175 ,
265 ,
845
{ {
int index =
hash Chey ) ; int data ;
} 3;
**
void Sorted Insert ( struct Node H
,
int n )
{
* * * *
Struct Node t NULL H;
g
=
p
=
, ,
t = ( struct Node
*
) Malloch size of ( struct Node )) ;
t → data =n ;
t → ment = NULL ;
*
if ( H =
NULL )
*
H =
t ;
else
while Cp Sd → data en )
p
{
q =p ;
→ neat ;
p =p
}
*
if ( == H)
p
{
*
t →
ment = H ;
*
H = t ;
}
else
{
+ → neat → neat
=q ;
→ neut =t ;
g
}
}
}
Void main C )
{
* *
Struct Node Hash Table [ 103 temp ;
,
int i;
for (i =
o ; i c lo ; it t )
HashTable [ i) =
NULL ;
temp =
Search ( Hash Table [ hash (21 ) ) , 21 )
;
}
* *
Struct Node Search ( struct Node p ,
int key )
{
while C ! =
NULL )
p
{
if they = = → data)
p
return p;
else
p neat ;
→
=p
g
return NULL ;
}
LINEAR PROBING
-
h (n ) =
N l 10
'
.
(
'
h (n) hln) tf Li ) ) " For
pogbing
-
=
t to
÷¥¥
.
30,0
26/29,1
gu , = ,
where i =
0,42 . . . V
30
z In case of collision ,
-
45
23 3 insert at neut free space
26
:
6
: ::÷i÷÷÷÷÷:÷÷:
74 =
( Stl ) Y 6
29$
=
-
. to
19 25 ? '
h ( 25) = ( h ( 25 ) t
f 12) ) Y - 10
Ty 8 =
( 5 -12) t .
10 =
7
11979
-
( 29 ) flo ) )
'
h =
( h ( 29 ) t t 10.
(9 to ) -1.10=9
cyclic behaviour
I
=
=
(9+1)%10=0
=
(9+2)%10 = 1
thing :
key : 45 45%10=5
found at indene 5
✓
4
Key : 74 74%10=4 •
Not found at index
•
Search Neut index until 74 is
found or blank
space is
found
•
Found at index 8
Key
: 40 40 t - to = O •
Not
found at index O
not
t
sine foe O 9
AveoageSuccessfaoch
= = = -
DRAWBACK l
)
( f- In
t
÷
=
*R L
X 11 Table should
E O - 5 be at most
AverageUnsfulSeaoch
half filled so , there are t =
I
I t
blank spaces and
-
searching
can be made faster DELETION IS NOT EASY IN LINEAR PROBING
PROGRAM
-
hash ( void
int int
key ) main C )
{ {
* *
Node Hash Table [ 103
return
key t.to ; struct , temp ;
} int i;
{ HashTable [ i )= NULL ;
int index =
hash (
key ) ;
int i = 0 ; Insert ( Hash Table ,
12 ) ;
return Linden ti ) t -
lo
; temp =
Search ( Hash Table ,
35 ) ;
} 3
index (H
=
probe , hey ) ;
HE index) -_
key ;
}
HC )
int search ( int ,
int
key )
{
int indene = hash (
key ) ;
int i O ;
-
-
return ( indent it t 10
;
.
}
QUADRATICPROBINGTHE
drawback of linear
probing is that elements cluster
together and
form a
group .
;§
h th) ( hln)
tf Ci ) )
'
f Li )=i2
O
to where
'
= I.
-
I 1- = O
, I, 2 . . .
¥!
" "" " A
I : "TY"""
13
-
s
h
'
( 43 ) =
( h 143) t flo) ) t.IO
6
27 =
(3+0)-1 10 =3
Average Unsuccessful Search
\
.
13 7 =
(3+1)%10 = 4 I
I d
-
8
27
'
h 113 ) =
( h 113 ) t f ( ) )
2 t to .
g
=
(3+4)%10=7
DOUBLE HASHING
;µs§
ht ( N )
O =
N t .
10
-
251 hz ( n) =
R -
( my R ) . where R is
prime nvm smaller
35 z h
'
( n) =
( hlln ) ti *
h2 In ) ) 't . 10 than size of Hash Table
-
g-
Where i=0 , 1,2
3
. .
I ; n
.
, = +
;; ;
= ,
95 6
-
7 -
4=3
7
-
h 1151=(5+1*6)
'
8 t.io = I
25
7 -
Itsy 7)
g
-
7 -
I =
6
h
'
135 ) = (S t 1*71%10=2
7- ( 35%7 )
> -
O = 7
( 95 ) 3*3)
'
h =
(S t y .
10=4
7- 195%7 )
> -
4 =3