0% found this document useful (0 votes)
61 views14 pages

DSA Searching

1. The document discusses different types of searching techniques like internal searching, external searching, linear searching, and binary searching. 2. It explains the process of binary searching an array which involves comparing the target element to the middle element of the array in each step and eliminating half of the remaining elements. 3. The document also briefly introduces hashing techniques which map elements to locations in a hash table using a hash function to potentially reduce the number of comparisons needed compared to search methods.

Uploaded by

Aayush Parajuli
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)
61 views14 pages

DSA Searching

1. The document discusses different types of searching techniques like internal searching, external searching, linear searching, and binary searching. 2. It explains the process of binary searching an array which involves comparing the target element to the middle element of the array in each step and eliminating half of the remaining elements. 3. The document also briefly introduces hashing techniques which map elements to locations in a hash table using a hash function to potentially reduce the number of comparisons needed compared to search methods.

Uploaded by

Aayush Parajuli
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/ 14

Seasching

Jhe pDce65 aindin he


sada item
Jocahon da peitic
wih givey
a
valwe ihdin4
he
more
oCathons
vndihong
d ay
na
eends, which 3ais?y
ist is caled
ais one (
5e chine

THpes
Intwanal Sear chn4
(). Eernal

Lneanal Sea rchl ng mean he nds


1
n n beys where here aTe le numbe drecids
e cnds
idin wihn he Cmpu Ber' Main memmy
Tpes
4 ineur eg veH seaschins
Binany eanching

a Linea easchiny
In near eAsCh eAch elementdan
array is ed O ne by one 3eq veqtally and
Lonpaed wih. L h a d e r e d elemes

A Scarch wiu be Unsv etes


wl i au he elenenks
r e e4 d ad hedesied elemet is ndt d
Adranagrs;
1 t i s the inplest aw ay ding
elemek in Ust
T seaaches he elemet 5e4venkaly
a is, no ute whethew List is Snks
Unsted
dis adwauges i
E's 4 Vey S1uproes
Tt is Used amt ld deta
ontu smay
IE is 4 Ve e o n s v m i ng method.

ALgoiLn
1
Setiodex Q
Scan eaeh eement a y dne 0ar
33 1 math dccw75 hen n h e indax
5tep y
dHherUi3P 6et ndey in der +i _gora gkp2-
4. eit.

Ipv an ary
4dnnelemett d "data
F be se4rched
Lni ialise i 20 and
epea hrgh 3tep 3
i i<n byin(remehnq ne
1

d ACiACii_
b 0t

prt dan is nnd 6e4Tthi nqis


se

Prit d aa is nt ond
5earhln is

5 t.
b in4 SeTch
bina
we trs Lnpare he
Ley wih he tem nhe middie poihn he ary
he
he e'3 a a y
matth, we can
retn_ imme d aky. 1
he e is
e s han h e muddle key hen he
h
tem
ngtt_mus t ic in he wewr hal d he
aa 1 t e dhan Lhe tem sng t muse
iein h e ppe ha!? dg he a y . 0 we repeat
he pmce dire
on h e lwer
he ru4
a
&ppes) ha
an
iientk algniiy when
Uneg 6earch bt he amy ha
obe Snted bere ding na ry4 se4rh

Adwantagesi
in a 6earthin each Conpan4ion2
he ize d heseasch ae A is ve doced by
hal , it is24stest mehod
han
i nea %e4rdh,

BisadAargs
Binary 6e4rch requires hat he
tens he a 4 h nid be 6 ted
Binary earch Cadt be vsed whee
here are mhs insesions r delehns.
O Initalig 0 end n-1 and
mid(beyt end)/ n'nonber
nombey Q5ntt
5ted
elemeak in an
repea_ 6kps
3 dnd 4 whie (begiad
and. amid!a item|
1 tem< 4[mid
end: id 1
else
be mid t
6e mid
5
(heqa end )/2
aLmid7 = item
hen
hen set Loc mid 4 displ ay*
vawe is mnd al De Lecak on
else
dsplay he dan doesn't eis
eit

0 d 531
15L 0 3 318 u89 64 591 47
mid : 3
end :7

591 4Cmid ] 318


be mi d +i Y

75 SI 03 318 489 54 591 (47


beg d:5 Cnd: 7
594 aCmid7 S4
6
beg mud t a 5+L

75 1SI 3 8489 5y 591 47


beg ed:7
S31 4 (midI: 531
o
is und at
Seavchi ng
Etieny
best Ave

ned onr/a 0(nt

Hashinq
Di2rrt eaTcAng Teehn qves ee
binary n e r are based on_ampamons
teys (data. But hasbinq is an appmh
mini mize he no Conp4Aionsin de
nd he de sie
TE is hat 6eah teeini qve Dh eme
00 nett esaryLompaions 4e quind.
JocaHon he
Hee Lhe
C pi le
eiee the dt
Aesi ed ree_id in d e r
ingle atte

Hash +able
Hash -table s6mply
is 4ddbessed a a hash vnchon
Hash tb te is divi dedinto a no bvtbeks
and eash bucke has hmber s l 4ts 4eauh
l o t is Capable hoidiay ne elwd,
JocaHon SLot

Hash ab le.

Hash FonHon
The basic' i dea in haghing i s h e
tansmahon a beyinto Lhe muponddne
Jarahoo h e hash tble.Thi isdan
a hash nchn A hash nHon Lan be

eind as ncHon h a takes e4


sp 4 tansrm it ino _ hash a b le
de. TEis U6 vally denbted by H

Most poulaz hash nchon


odslu4 mehdd Divion MeLhod
Hid 540@e meLhidd
FoldinA meh1dd
n_ihon

d o l vS eLhid
his medhod treany- hash
onthon, we map Ley one
+ n g h e èmainde lQvided ded b m
.e HL) k md
Let Hash able
Insra+ Y5 333, 7

+hen,
3333
H333) 333 I = 3

H1R7) :_122 n :6 S 456

10

HASH TA BLE

MID 5 90ae mehhd


In his mehod, we54v4eLhe key 4{tea
ethn4 humber_wehe som dints mLhe middl
an addeK

Aple
InsesS*. 1, 13
1 19Y 4
124
34 13y I7 956
3

44
foIdin4. Mhod
0ne he eaiet way +o Unput
he eq 15 o b e a he ey Into
bey he cesadd
hem et he hash 4d dreh
eaample Let has hasle size 6d
1234

46 50
0 4 is he hash Eey. And
a 34 in-hash table

25
+a6

hen hash E e 51 -50 1


Thenn 25 in in dex no 1
iSon Resouhen
we wd - ad d aneu neloY d R
S Upp04é

ppose Jhe memmy locakbn

dddtress (E i[ al-ready oCCujon td 1h -uahon 1S Latue

Colliion m Ire. d a
d o l i i on That
,
3 occis the

ime h ash vawe he. Lagh a b l e


F map to
SlLe d
oLUSons

ColUon reDLu)HOn pe addre ng

Re has kna
pen ad d r e i na
Hashu wi
n e a r p ing
1adakt ,
D ble hAn4.

This m ihvd u ses vhe hash

h(,'J ('Ce) i) mud m

ishe hz h kagh table


whe
h () L mod m
key vewt
ns d hseina he Leu, 6 37, 59 7, 45 8
ash +tble e m !! D si n Un ea po4.

CAnso d Me piim hash onchon sh'(k) = emo0d m

Sol 2 3

26 34
serk 6.

h (t, iJ h l e ) +iJ ma m

41
h 0) 8 mad 1l +0] mod
mdd 1l Y
L+07 mod i1
nne T C4] is easen
inseab 37

mod I1 +o] mod 1


h (37,0) =
37

SnLe TCJ is nd ree he ose seq veat

npokd
h(37,) 37 mod t! 1] mbd !

inte TCs3 is e iut be 37 at

So
ia
dthe
p siny
meno UGes
hash nthon
Thi& d

(h'C) + C,i t i) med m iso to.


h(eiJ
mod m
Whe h(E) .K Con8tanh
a o ai an

6
Consi de he , 6 37, 59 ,1,
as h rble 11

Lonaiddy 4ha Lhe


(L Ca =3.
hn h'L) = ae
e
7 br n n , m
m t it i
mid
hk,i),
T CR 3

mod m
it 3i]
h (E,i)-k mod mt
mod 1L1
. seAt
Li t 0 t] mod
mod
h (76,0) :(76 lo) m0d 11

0 tiis lae
he Eey 76 at
nest
TCi0] 15 ,

Dnble Has un S ne he bet meihi dy


Dble has h ng

qval able
6es
hash ne he
Dnble A4hi ng mud m

(h () i h, (t))
h (,') Lhe pimuy/ aueilieny
hash
hash
a*

h)
d h,(E)
Conscer esthngha ays 7 , 37, 63, 1 5
h e hash +hle
U ng dble hashing
Con der dhe Pnany hash voion are h (k) L mod
h() mod j
nse 626
h(7) : 7 mod ii = 10
h6)= 7 mod 9
h(76,0)=(10 ox4) mod 1

h 6 , 0) = (9 +0x?) mod Y

nse 37
h (32) 9
hCa7)1
h (37,o 1
TCY nt e
0 h (37, 1) (+ iai)med *
!= 5

Hashi ny wih Chainn


This methdd mainai is -he chain, elenens WlU
have 6ame hash addes we CaN ake h e a s h table a

4ral akrs Sige hssh +able Can be ncmber


Here eah pnen w i phrt one nked i s t

and he elemey which ha 5ame hash d d r e wi be.

u i ntai ned Ln he Li. Ut we Cay Mai tni htlittd

n 6rhed daty
Insest
8 19 /5 33 2
17, 1 6 o
a h a i npd ash rsIe. Let oS he hash abie
spp
Sl0- and -he kash
has vn ion be h (e) =k md
The ns al H
h e chainrd - hash able
S
h ( s ) s ms 5

5NVL|

nses
mod 9 i
h 2e) d& =

RZ7

13.
19 mod 9 !
C19) =
ehasi ng
The a (hanves d aserti en ilure whey Lah tahie

ovtion q r dhis Cuse 15


50 vhe.
Lable iLh Lhe

Hee we win Ve new ash qvnttion ánd


h ash aLle
eelemenk iy he preving hauh able.
LS Es+

teehni q ve I6 Lalled reas hina


Thus
+ate a Lase whee h ash mble ze is
Le Ds
and hesh on
inearp-obny
ian d we de
06
ding
elemeak n he
mod I/ The
S H() :

ash able 4 wn

5,u6,{2,10, 9,72
, 8 3 10, 3
hle) : (h'lR) + i) mid r

8
66

43

wnst 62

h'l)= med m 4
62 m0d 7

l
7t m0 d ewi ake nestdble
Aln n table is . 50
dable wjlh pr me ndmbe he nexs
n re han
is 3. 3
nvmkser t ditie i e 12
me e
asi wi be (akd i z 3 d a
asefrd ew
elemeals wii be

HE) mld 3.

You might also like