0% found this document useful (0 votes)
12 views

Twodarrays

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)
12 views

Twodarrays

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/ 1

Binar ySear ch:

Unl iket hel i


nears ear ch, wher eeachel ementoft hear rayi scompar edwi tht henumbert o
bes earched,bi nar ys ear cht echni quedoesnotcompar et henumbert obes earchedwi t
h
eachel ement .Hence,l inears ear chi sus edf ors mal lerar rayswher easbi nar ys earchi sus ed
forlar gear r
ays .
Thebi nar ys earcht echni quehasapr er equi sit
econdi ti
ont hatt heel ement soft hear r
ay
mus tbei nt hes or tedor der ,
pref erabl yt heas cendi ngor der.
Thi st echni quei ss imi l
art ot het echni queofs ear chi
ngawor df r
om adi cti
onar ywher e
eachwor dcannotbecompar ed.SEcondl y,t hewor dsi nt hedi ctionaryar eal phabet i
cal l
yin
sortedor der .
Thi st echni quei nvol vesdi vidingt hear rayr epeat edlyi nt wopar ts,
henceappr opr iatel
y
namedasbi nar ys ear ch.I tinvol vespos i
ti
onvar iabl esl,uandmi d.Initi
all
y,li splaceson0
wher easui spl acedondi m- 1.Thef ol l
owi ngs tepsar erepeat edlyper f
or medt i
lleitherwe
encount ert henumberorl anducr os seachot her .
1)calcul atet hemi dpos iti
onw. r.
t.cur rentl andupos i
tion.
2)Checki fthenumberi spr es entont hecal culat edmi dpos i
tion,i
ffound,pr intames s age,
updat eflagt o1andexi tf r
om t hel oopus i
ngbr eaks tat ement .
3)Ifnumberi snotpr es entont hemi dpos itionandnumberi sgr eaterthant hemi del ement ,
updat el tomi d+1t os ear chbel owmi d.
3)Ifnumberi snotpr es entont hemi dpos iti
onandnumberi slessthant hemi del ement ,
updat eut omi d-1t os ear chabovemi d.
Ifl anducr os seachot her ,wecanconcl udet hatt henumberi snotpr es entint hear ray.
TwoDi mens i
onal Ar r
ays
At wo di mens i
onalar r
ay i sa col l
ect ion ofel ement sofs imil
ardat at ype s tored in
cont i
guousmemor ylocat i
onbuts t
or edi nt er msofr owsandcol umns .
Thes ynt axf ordef iningat wodi mens ional arrayi sgivenbel ow
Synt ax:
dat atypenameof ar ray[ no. ofr ows ][
no. ofcol s];
Ex:
i
nta[ 3][4];
Whent heabovedecl arationi sexecut ed,s omewher ei nt hememor yat wodi mens i
onal
arrayof3r owsand4col umnsi screat ed. i
.e. Eachoft he3r owwi l
lcontain4col umns .Ther ow
andcol umnsar enumber edf r
om 0i ns teadof1 .Whenel ement sofat wodi mens ionalar r
ay
areacces sed,t heyar eacces sedi nr ow f as hion.i .
e.f oreachr ow oft hear ray,col umnsoft he
arrayar eacces sed. Eachel ement
ofthear rayi sref erredi nt er msofr owandcol umnpos iti
ons .
i
.e.nameof ar ray[ rowpos it
ion] [col pos iti
on] .Forexampl e,
a[i]
[j
] ,wher ei isr owpos iti
onandj iscol umnpos iti
on.
Mat ricesar ebes timpl ement edbyt heconceptof2Dar r
ays .

You might also like