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

Unit 5

Uploaded by

2k21ece032
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)
9 views47 pages

Unit 5

Uploaded by

2k21ece032
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/ 47

,o

oNlr -5

Sor,ting ond Se otching Technigueg .

fnsar.lo crn Solb


SorE r.tjneoi Seovrch
Q"ich goU
b.nrasrb geo,yrayr
Heop Sorts
rnerte Sorb

Jngq'r,Jicrn soab

- J.r. -i}, AimP\e Sor,ling olSorithrn


,.bin
Jnserr&icffr Sor.l trorEinS Simi \ oJt
Jao
- 3o.6
P.^"t;3 coYrdS in hondS
- 5& \rl: 4g,urr€d &tof ihe { co,xd o sor.!ed
trles unsol
.r", +.tg CoYrd'
c rA t.Ieole}l
cord. a.{ Se\ec&Q o'(

*he cqlt o\
ploced- ob +he
ri ide of pJoced o.t +he
ur,l\ !s
. Simi all .rrn s o'r &e d cct' &g oxe
"\.t.E '
&ajRe.n rr", .trnerrl eocacE P\ocg
,.thg
Firs o.Ee one e\eroenb , iEero.tsg 'Jt 'trlnrotJ'tft

Scrr &Qd o'r{o\5

OisqdrnoLqge
gebs
rr,.ir not atProPtiole -\oY 'lortg do'to'
Sotb +-tng
- '(ime com!,l.oditt o,6 .ri'tsetr'ftcrn
!.tfr

q,veroie corg qn.1 trorgE eo>e


- fnscl&ioc' SrnE .u "tu,r attictenE +^o-n -he o'\hert
9or o.r oritlrro s \'ihg
he+ SorE quicR
I
SorL I metrte Sortr ... gg" .
@
Bdvon&o$e

- simP\g lmP\emen&olictt.i
- E$f,;cieni -lurrr smoll diato Setsg

-\w dofo' seus ++rc.I oJlg


- f& j.l, olPProP'riof'e'
eot*'ed''
olleo&tr Subs'rqrr'ttoll-r5

A\3o.rirrtrm'.)

s,leP a : stor,t- -$1e Pto$Tqm


+,e Fi T S.l e\ e rnQn
t t a A urrie- Jjnof
E,tQ.p 2I :5 Jche erqment ..L
t-
ir! .* a\reo.\ gor,led\ ' osd Tq&(l.ttrl

anrd' s&ore it SePo.et\5


S.lep 3 Picts 'Jdng ne'cE e\ersenE t
; o. ke$.
.rn +he
rrittr crll. eLemen'I a
S&eP c'. Nou) r co(rrPclxg lrhte Keb
So'r&e& arro$'
in *he gorJEed orrort "r'b A,molle;t
S&ep 5 ft &r.e e\e$en& r$oYg ''l) +Yre
g\qfr\enE 'tsnen
+^on tilng cuJstgnt! '
nq.Jc\ts e\emenE '

g\Sg: Sh'{& ts\eoE{ e\erNenE S in +he' or(a'(d

troulo:til'S glrs tight'


'

St,tP bj Jnsatt& 'lf\e valrr!'


'

'. Aor.led.'
nrC rr}
: Repeol rT t&'.rl {he
e'c,
S&e? 1

6&eP B : s,lch Ihe P"o$rom '


@
o
[il -l
I 2
lo
3
5
5
E \ernQnt
€-\err'ents i3 Sorted
Q
Bg d%o,t-t, ure corr or''i3n First
o l\ 2- 3 \ 5
n qoq Sis's , on\$
-\
ong
tl \ \o
i 6I L 1i.-".,4
-'lin in 5ou'r
\isb' ib
Q tarnQn L
Sorted
€__) I e ",-"5s
Sotted rrngo.rled r SubliSb - wo need.-to cdm P6j€ .^li+tl
sub\ iSb ar,\u Othe.r, Vo{iOrbl e lvqlueg
\ooP
- rut"i vq\uQS alruo$ Sor bed
-) uuQ Cqn S[oJrt 6rut
,$ ;dclt I r:o n-\
r orn
j.x 'Io adn, J-n
th* v ojur.
ternP .r: i th
0 r 2 3q 5 c
5 q \o (, q1 o-r
$a.
L) \re eqn Oy e"f r.uri Le +h.l^
-$ P
in "ri3h
SY'.rtts

?
L tb .ri

I ,f
JI t{ sPac.r
-Ih
e..e o1h eyV eCrmPore
o L 1
L\ E
l!
ot ,{i{s,I Q\Qmant
.ur urrtl &ohe
5 -> BXo6, tfsE arrd uJg eGrt
o[ un s6rLa& .tomP
Sorted rrnsorted {cJuo un
SqblisE S&o'(e +hot
SubliSE,
caoeidey *["oIU ber.Y
dree

-> c.rroPonQ *heB {rorn arf +he


.gor.ted ,l urJ Jrrsb

rrroYe t-* rigrr {o J"TT

-,Eg -; rvo -) No noQd iro


vaJru-.
Shitt +he

Ll >lo ) No -) No oeed &tc


S\i {t the volrt-l-
O I a r3 \ S
€9
bQr'F
u ll .}atsQ
' SorEed i qY'r gcrf I eJ { + 6 3airr,
"
u]o ''t"il
Sr^b\iSE Sqb\f SE ** "rnSor Led \iSt
{\'
Bns,ider
;'-:'il'::".,J"" """*
c Af,.f)
ho\ € \ urQ
an
overurrilg ,xhe. valuq
ut u,q u,anE' Jn tomP '
o\2_\3\9

o
E

3
E
q
r IO-ghi$t
fifl-shi5u
sh,^tt 'rh ti3hb
comPole-
-> -$"roro'tt3

lo>l
hh

+ shitt
to \e6t

t\ 1 E o 6 o\
l IBSA , t

ot
1
2
q 5 \o
\5 [fl- shi$b @ * 'tgvrt P\ace
1 ohsut
\ix antslrhQ'( vqlu is E-) NS _>N o\, ' urg \W>t .r.r','tnAefQ
\n ssr!Q aSu bt tam? !q\ug
4t the \asb
o\ z \ 5
E \o 6
\
I

I f..\-'1
o r1 3 L{ S
-o- tamP

E
,l-
\o b, --)
Lt I
o{-

Sor ted' st'b u.\ S o"r t. Sub\iSb


\iSE comPo'lQ
o\t-3 l{ rr ro)6 -) shi S one Plocq

t \ \ C.
J
I
L;
lo 1 ct-
reptace @ un ttrrr' P\oce
5>6 -;* o sbi$ts
ri3hE

q>b -) N o shisb
0\ aSqlq -;.1e sti gt

t.emP
Sol ted \is
-.-J-1d. ro) 9- Shi SE
o\>\tts
q 5
I 6
?
\o I _--.--)
--.-)
6>2 shisb
t 5)z
5
0 I 2- ) Li
shfs
i\u\ C.
J I
1 5 -..)
E

0lL3q
It 1
\5 6 T \0 lq)z Shi 9g'
0 3\ 5

3
Td I lo
l-'-r-
tr >)- -) nO nee d .!o shi tt
Val!o-'
+he
5.5

ano \haY E:comP\


g
ota 3 t{96'1
E a
\ z \ \c \CI\T T-
E
.o
o\
3.
13

13
lo
L\541.
FT,l-'frl
\ 5 61
LQroP
I
lq q 1i3ht'
t_ T \o l*TTTsl-61 t{>3 -) shitt
01 /-5 B 6-1 tr ernF
a
J '1 q b

0 I 3 qE6-I q <e- -)shists


L_:_ tr \
FI,r'F-] 3< o-) E

o
2_ q
56 'i
5
(--..-..''-....__-) rO P
sort ed \ist r-r SOI
0l )-5 r{5
q \o >\
\o o shi tt
o I )to
arnP td\

Sor be D>t 2.
is uri s o.r te 4 t{> ) 12"
No g\i$b
S'u.L tiS b
3> lz
ol1 2) l2-
rl 5 6'1
Lt l& bemp
I
Sorted . <._-\
Sqfr.tut ungor tQJ rS
Sufr-\isb ) l ->shi st
o I a 3 q5 6 lo) r ->shtgb
1 l-{ 5 roln W q ) I -\shigb
3)r -\shi8t
bu.b 2>t -)shisb.
o I a3 \ 5 5 .r tr tn.?
Ea>5-) s hi $b
ti,D.l Sortad\Et. t 0>
Ll)5
5 )shig E
-) nyg sr-,i5f
6.6

hoSrom "{or llngut.trcro SorE


pnintS ( " Bctore sorEinE
( s'rdio'h) orro$ etemenE g arg _\n");
It inc\ucle
g inc\udQ 4 sg4\\b'h) FrinEArr (a.n).
instrr.t;(a,n).
void insQ{E [tnt' ol'1 ,int
d
hvinu $(..\n R-tFr ,lor.ting G't?.rU
I gtemenE ore
-\n
,t
) ,
inL dr
'i . &ernP;
Prin! Q11 (o.rr) .

{or(c=\') .i(n; j'++)


retqlrn O'
f of.il,
&a.rnP
=
j=.i-t' 3
dutPu.ts
rrrhr\e ( j)=o 6d &emP < ? oCSr)
Be6ore so(,hn3 q"frob e\einenr8
f at'j] ' drg
o[!+S = -
j ta 3l a9 $ 3l t-I
= \-r'
j h$nn Sorbi'.,A orrot e\ernehEs
qre
oL5+tl= &ernP; -
g 12. 11 2s Bl 92_
j
1
inE n)
void. prinLArr (ine o'ta t

I
inEj;
{ot[.;=oi'icn; it+)
Ptints $ t*o/o4"t aLd, l
3
.rr,u moir'C)

I
inE qtf = g,3err1\ '
frrr3ttaEt
inE n
= sizeo[(o)/ sizec5(ogo)
.
L'z: tg-/o sizeo'5(6)
@ / sizec6(u)
s,1

c
Ol23t{
arl
LJ-
\z 3t \ e
\
32 \a

n:6
g oxe -
p1\nt -) Be{ore Sol.irin3 a'rtab e\ernen\
q5
{.uno,rr'crn .z-all
o
Prinb Arr( l1 ?r\rs 8\32\rr ,6)
{o rlo $urnc.xcrr. d %,r ;tr'crn pottb

L,\:O j<n pn inE .r; .rtl


o o< 6 \a i=\
r(b 1L 3l
a<6 \a 3t a6
3<b tL I:
q(b \2
5<6 r1 g 3a I

6(6 + {."'rn
ol 2, 5
insqn 25
/ $o ,.to {.unc rtr'on dn4 'r,.f-r'on
po,rE
i" ftr 3l

--o 66 .bemP<: al:


{n = ALll j: -> w\i\q >
T 3\ < : aEo:l
r},etnp = J I
3\(: 12

*,'--.t+ | I14 F

\
\uj --=-r
tJ of.+r] = .EQmP'
af[= 3\

.i.:9-
[@J."*
.leroP =atal
.fernp : J.5

j: &-\
rE
J- ril E rQfnP f5]
t_^.) i\e ( j>= o 6$ "ternP( -- af j ,] ri @
l):o 3S z5 ( =afrl
T 25< = 3\

-r \'r LrO
T
-\*\'ll. \ .^ ,i ,\
\ oii
a1r+il = atrl
attl :3\ 4::
j = l- \
o,

( j>=o 0A .la *p< = oC51!


rrJhi \e L
b): o 63
.15 1: ato]
.rc. (: \a
1
\rFAA e
$sajle
af !rl : res!''r
aforD:951
o fO '&5

-) j
jernP: I
: o (.j)
"xe*p
ue.n? : ? j = ?-r j,E
36 .}ern p( o'cjl)
uJhi\e ( j>= o

b1: otl
EO T
A
s
1
gr\
Ot E-j
) 3\
I
1r uO

SemP ( = a(.il
oLj+il "o[!l :

" [>7"]o* <--25


af r+[:al!] -\ { u-[

o j : i\ J -',t u,o
of rtl: aCtl

\
aI (

I 3

O): c.) e& 8(: o Col


l2
l:)"t1 3

-r 0 ('r& J: O

1
U
a o+\ -- a C"-l , ? \& 9-t \l
a(- : t9- ) l
Qurcn Sor b
ot a a \55
lo r5 9_ q \6 ll
\
nE l<rnor';n aS ke e lemenb
tuQ con ehooSQ one eteme
-; Bal:ic idPo -
in +he \isE
f got. co.n chooSg
'
anq g\efnenE
qg pi voE l
e t e.ne nL lq)
etJm ent \ov t r'n iddte
'YheY -bi'( s
ei E

ond etemenb J r
ong ra6dorn e\efient Sott'

tr)6nt 'rt) Select


o.t Pivots gtement
1- am c hoosing Firs t ol emenb

: \o 19b gtemenL of ot*


pr voE -y

-t Quick 961g j; bor,ed on a.o9 ulrt

&&
lnd ivide - co''q Llon t}P
alYoS,.r.rrto Su-b -q qlro
-)
NouJ ,t ,h) cfn
-3 Tou

If:rIE
r
\)
JIIA o A\erngn,-b
q11 +,e 9t
piv o\ts
+nQ Pr
P\o
e \qlathts '
Si dQ go p i+he r ula4
,l_!tr r rghts
It it equar .rco tln€.
og e\amenE
lett '
P''
L

Eqere".,vrf I
J
?os-rsfion 1 Pivot oO .2
PoYrJnts

Vd!&S < PlYof


J va,l.uA;pivoB
egua,t va,l'..e mgve

l*.o, not Sran&ae 4rof


t- &tt, rrght (^ios
llua pcsr,h riorn L v afrr4| oJrG
q"bu
in lrorQd orote\' a agarh , ,uA- uJ'I
)>ame curcePb
in par,tr'l{ott {,
ParJ: rr on a
otaSq 6( 5.lo
\o \ \5 \ l q rb\ rr'"1

o\2- q56
l--_
t"- l"\rl
,l
t\r \0 t

1
valu€ a-nol\"'Qr
+'.l.; P1r sg
thu;r !r\
onQ Su.burr arJ
J ons s u!arraX
Nsrneed@ AorE
Capft5 gar.,e PtoterA p\vots va\ue
{.rdfn o'ro 1l t a'PPI! SarocqPloeel^.
{roro Jro 6 1
1 t5 r6 rl
J
Prv ot'9 -T
t p'.voL : rb

f:.arr.'P\e-
o tz3L\ B 6-1
r-11
8 "rl uou 4'rnd' oJ\ e\cmQnL-
''oi a*"n' +non *vru
\o 5 q L and
1 b _^J
I
li.rl-"*'"nb ' \^,o G'o6ts
T pivou = o.Lol iend I
ii* n,n,."ent
by dlt to
s'roP '

-'r
b \Q
\ 5 c a f$s-T I
Stcl)&
P\voL ,l!
-
8 +t'aE , eqUo\ g
-1-
-1
\n cr em.Qng
eA
in Cre m ent
15q g6olt
l . Cr-,6ve gU nQtE
-1 \o : c 9_ \5 a
\
?,Yot SLcr'lf
neccb
b1=a -) \ncleroef,lt +t,e{5'till ,rnove '9o
O\:-3t4S6r I
r-l rO 1l
*
l-
P,uot S.t-or& 6t"pl l-r)=+/
L) s,r op .t" +t rl X Stap ,o (

elQmenb

s;t(orgtr ^t3,
@
\--1 St S-OP

I
-1

I
A
? q.

tr \
\5 \
I
o +)l

T sI
oj\r

IL r<
,\- --al-) Ye] + incremQnJ ereroenEl
'J f MoVe SI "-IT"S.
w .1
5

Slar&
Fl-"1-{c E
to
Q\QrnQnts
[ta]G11. move JEo oecc
\a-ft]-fiIzlLt I SbanE
urg. ge e
1( =+ -) FaUg,
Trtu 1 e

mj.r.l
TIL\^ r cnOvq tO nQ'(b
o t{

-.f("Cuf,atol)
-1
2

E
S 6-1 I
b 1 J 2 IJ lo

Storrg
L;
l< =a -) tf)ovg {.he g&o}t
a{rorrJ b$ t
,.<:a -) movg +.'S S,lowb a'trou.l b$ t
'1 5
lqI 5 \o
a 5totrtr
9 (:a I r..,o CScoP)
3o
j-o nd pr{oA)
S\toil! rjor r'alr(o {.roSS ? -+ S\r{4 ?lcca-
Q Vosd
s+ar'-t $nod 3rea tarfhan.tD g..Jaf
enat',go ^o.D ^ot Soin!
fi,r etangnt
@
e-n d
oi[L .trt ,fa)'A +l^'aft
Y\o-./ e s\l
guro? P'(ocQlA ,}. .rII tlo-
Fo\\oo +he
e\emenE)
end]
st'e[i"o'r
lw*t' s,te? ) s"')c'?

t*trrro \ I 3.q 5618


q \o
1
___r_______J Pi v o\: &\-'a-n Lor )
Qq ual
i c)r)
6rq J-q>,\
s Yr
c ts,ech. aU +he q\ern Qnts
\.-.\--..---......./-
ar.|. the e .l-a.m ont .i^ +t'. e vi3rtt sf&
iYots Q.\Q cnQ n
..r, 3xeareY '\l.taf1 P {
NOuJ r 50[ '56a"\' rlCO "PSt &ttr PosiHan
-lh,,o- r.r\q .,}' & sid\
vi3xt posibio'n

c o d.'r''
e\1 or i tt'.rn Pcnt r
f)l.).,*o*a
d =O
Fo'1 suao [f ,db 't.^,n*, oo* A ".1ou'rurboun
LItPo{ bo rrncL : $
f eT"rorl

pi v6u = of.rbl ,, -) choose *. )


J,'"'::1'-5i:;][:**.a(up)
S,Lor'& = &b ,
/'>
t! q Cs en A I
hi\e ( o1s.r-'l (:- Pivo .E)

I Gtt\L t +'
4{ Ug-
S.JE

3
u)hi\q ( o.fend) YPivob)
fend --l
\
( A.lor& ( end )
"t
*
r-*( olst*''ti, o ge^d'J)'
("lp 3
q
{r.t t o.f r
qf e' A] ) ; $ertan €-nd l
@
Q,^teh,soru[ h.lb r rr\o)

fl
*t Ltrb < ub)
f.f,oc ( a t tb I ub)''
= bor.bticrn
Quicnso",u ( c, 'rb, 'Loc-r) '
Quicxsort (61,Ioc1l , "P) ,
Y
!
c$ o-theY Eccomptg
o ra3 q 5 6.1
f.-1 T. s 5 r,
\
pi vot
fr gtoJl.E

p\{ot -- +
r PivoE
1St cornPorlQ
s&
r o(! jncrerne^l-eJ t f
-l(= t .r.-o- ->
60
2 (--1 )
b,
r<--1
(= ar Lr^a ) shif
movtn3
go].be r so 5,!r:p
-1
ot l{ 561
3 \ ( s-t af,rl I .1*: )
S bdlt end
5(=T -> shi sE
6 5 15 t (=a + shi gb
tr< =1 shi w
St-qt
S+arb", *4,
FI na
S"J4 en d, Y\vor;)
\3 L I 6- lil-T 8
*1,
J*t^4h"-r1 P ivrr
t-
3 7q te',
lL''o-'
@
Q.,ricn. go'rE

lanouj n Sor.tln3 olgoti*hm


-) Qurch Bpr& .rL +he '{o}tes"t
orgorilhcn o^A ir ba:'ed' ar\
- t\i3hei6jcicn& -$o"r'l'rh3
ecrnqurlt s,J:(o.tse$$
divide ond.
+he One 9\emen.lt colj-e&
.uic\q b r urQ .loh-e Jileg
-)sna Q So'(
qrvar' +he 4oro
u'e UIE Lo'lJ tQ
oq. p\v ob ' gteo.loY +t\on o'
voE
a P'
and
Smol\ el eg\eroent
lfton rh
-q b\orr€ +t'e P\voU
pivob l ct{&on Po:r'rrtrcroun$
+r,e -$jn al Po8'ilf,qn '

Jri& 3 m orn FortLe -)


A,rxori+r'm aiuiard 'dhe
\vot' e\Qmenb
Q e.remen'l s Je1b ,bhon *he P
( censro'l e\ements
Lorl SirsC e\ernenb
@ p',rou e\ernenE Lov ) end
e\ernenu)
g\emenb
rrorn '\ho Pivoc
E\emenLS t$eolQll

A\Soritfrro clivide- - cctnqtr.e,l SJcrof'e?9


-
o>' ttr e PivoE
vorrg ' "l* &*" *$,., e\erncnts
cYroose.A !ivo'!c jt jn ronXe a$ so't}aA
V olrra- lr)f|id |
Volrr-l.- l

Eo +tno-tr olt e\emenb I rli\tr vo'!.rl^ &)b


@ neotaqrr {he \ti-!b pivor! r$hi\e' a\\ e\emenbs
come $Q$org }tY'Q '
{tron +he P'tvo& ccnne' otbe)l il
ihon \hg pivos
vorrre g{Qolst'
tuith
Cequor volrre> con to eisoe"( -*{) '

t 4rf^e pivof 'ri '!l ,.rts


Attelt Irrtixpon'tt&iarr."rrtg
..zot'Le'& polJi'Fon
dPeroh'arn
Pos-qHcto '
rttixu)'
@
g
3'Re.,r:rgivqt5 Sor.t *he Sub- ..us.t' Gb &AAer eternQnb
ond *he gu!-&.bE crb t.Iteolcr{ e\emcnlg '

The comPret\i!8 Gb q ,uich gcrr.ts olgot\tt'rn .'io +t.)g


'!fr'e
tusrg& cq-\e "ir O(nr) r\ un Solie J e\ecs,enb
.
->

!vt leno en f 4h'of)

*t includg (sudio.h)
/f nrirr.ttng o'yrob trst codg
voi& ?vinEnrrog (inU {(eiya$r inE
f ")
gor(,.drt .i.=o. .i.<n
;,i++)
Print$(.,o/od.,r,
brinUg(s 1r,,,,1 .
j
llth cR soY
void. e E( ioL $ireu , int losc)
f
ln..E orJ P,
t( otb )

/l gec pivo.l etemenb o 6.rrr,rU


piroC; {,r3b ,
"i = SirSU.,
J=lqrbl
r.ohi\e("i<i)
f
ll mo ve+ete qrhi\9 .i[{irer;) ( Pivot
rohit€. ( o*rtc3 ( = Gltlr Lpivoul dd
i ( JesE)
,it+
5 .lg
//A.tove ri3hE Qhi\e( i(rotu) Ypivot)
roh\\e (awC$) ) otr g Pivou))
j-- l
*t(i<j)
f
'!mp ; orr f.iJ'
arrldJ =ortlrEjf,.
oru CjJ : Lmp .,
st
vtsmp . orr:rf pivou) '
ar.r f, pivoU] = otri ESf, .

oruCJ) .Imp .,
=
Qtuck.g65 (_"xr,,.tr.ir^rf j_,);
,
(,(*uchsq u (orr,
i+ r, Jo.bb) ;
33
voiAmqinc )
f
in"r N\[o) , i' n,
prinL3 (" Entur .ro. crb . etements S:\o " ) ;
Scqn I [,,o/o&,,, 6.);
prints S (.'
E*.rr +h,e etemenLg :\n");
{or (,i: o; .i< n ', 'i + +)
f
Scon5 (" olrJ" ", d.rrrorS C.il ) ;
3
prinu.S(" rJnSof .led, elemertE S: \.") r.
fttnU er.rog loxrog , n) .,

Q.uicRSo.C (orto'5, o r n- r)'


prin&$ f" Sof.tod .trtU elput Orrick so..,,ts:\n"),
PringArrog(o-*J , .),

3
@
OulPql, '. -)

Entex no.06. elemenb S:

5
EnEerr -$ne eLemenb8 i
It
e3
55
r5
t1
unsq&c{ eteroenL8:
rt 2,8 69 15 11
Sov,led tis& ot-rerr Qujch Sorts
I

ll l5 \'1 13 s5
ol ,- tl
F"l
3_

t tr
t a3 5s l5
ol o
voidr (lrricRSorE ( inL iYsb b)

I
f inb.i o
tT
r
)) pivot ,dirsts

) '"|.o.bL

ue-

Pr
rt
j

;ile--oaqn
.|,.r.^o
rl )
ulhl\Q (qrrLO< : arr fol d6,j< Ll (^)hi\g ( o:ofil ( -- o:utd dd I
l\ I :. tl
6d@ 23( = rt
Fa.Ir) g
T .T

\Yu.o
J 3o ,fo trrhil \oo p
.r-.rtt_r
:o+\,
\
@
urhl\g ( or,lr f4I Y oxn tPi'oc'))

-fr !ro-

t
j--
l'1 >

J-r l
Il -> T.rLq-
@ pivot-
^3u

trhi le ( 2 orr f ptvotl


":ul[3] )
1l u.!-
\5> r\ -) TY L\Q-

r)
o r,\ \e I "r,r[af>ar:.fPivoE
59> rt @ olt-3t{
\r u.e_

J:l tt l5

uhite I ar\]rLrl 5 arorf er'oul )


J
r3)rl
rr uO
J=o
o:o'' CPivorl )
or-,i le I ou 1o1 1
rl >ll
Falte

(t<o) * rtr iE
FolLg
EOrn = |l
,trn! -- orut[O]
\LmP : tt
oxr[ol;, a*r f o]
arr lol : tl
arr fol =l\

J
Q*cksoru( alr) 9rJ\ -l )

Qrrr,'onsott / aNn.y t,, q) ) ao Jd Q,Pr'c'kgor b


\
-(1tunc rtr'on dr{ir..ai crc,

Ir 2? 5
@
Hee p Sorb
I
Hea P *t .rr *he clee boted doro' S''ktuct-ule'
&&$pcbq6 Ho.t, (
xat tleop
1-1;6 Hoep

. rroce \eap fnln heap


o.boC even5 nodQ i, {ne v.--ruo
"tor everr6 nodg .r.l +hg va!r&
crf, node *j5
"Ler* ,llnon 91 guo.I
p
{1 "ode .rt eota-rl UCnqn oY
60 ulA pqlren(t Vo'rrrQ ,
.UO.\ JF olrQnb v
,'E'Y.cePl: \-oo t-- 'r \
Rf Porenu c."!1 l nti
$ poxerrU .rb
B t il l
o

each o o
u.rt
3Er
chitd no
rllc-corUq, .ro
nob ho.rrr-rr
penenh. . 3

_) erer,$ n Fqrenu node shoqld.t+


ua hqrr io
$ v.t,r" .tot: .t3non fo1 )
9v€orer +hqrt oy Aq,rof 8.
1,,af qtrc
ott'.&S to clrildnoclg.
d€€Cfi*e,;t, vqlue.
descendqnb
gT?eler -> d'.F.freqtP,\ q)1q. aU r'r1
{.e}
e d4. ortcJ wka
7o> $lQsr r5 rq 1o 3o
t o oa 6 ?
Uou uri{ &
r.fi-q, Sit,.rrCd ?
la3 55'I
tsol.! lisl aslaq rlo q

t
6 LO

gtemQrt't ur'l Hae o+


llow rfo uhsqxl +he
.i^ -{h"il rroe heof
f. uron,t 5e $'sen&
o LS s 618 9
to q o
'r0 qo| ub
F] l5 6

w
5o

ot2
-10 bo qo q5
55 t
5o 31 rb ,to I 35
J
agoth@ n comPonq
rrri-th porrenE. .

isb comporrQ 6o .rit" +o

Por,to.n
-J ccrmFo.rg 5o > (ro
tlor.r Conno& ,ri'S qn& *hQ- !lo
e\emerit .h +t'tg '(ooE, On\s Str.np {t.e
ura- oor\ !rn's alt .iE jr: .taot &po8ulrcrn
node. .

) owar, lnseyrt.E + .<hedc ,it.tt


uA pUte U,incrug &rea..
tlo
T.,llqtr o'ucntrd
Joto Lt5
$rom at\s..I$t
$Poss;rrte-.
?o 5 6-r
or 13
Ito q5
q
sol3q t6 to
I I
10 9l 35

a$ 1 uarg. ohoihe.( etements B .;


+he .!ra.e-.
chec\c' >6"1 aNO
-)
$trtcr? *h€ numJ.ra.r'l
laO

?o

1o 6o t{o r{5 50 31lrb lo 1 3s 5


78 t ro
olr- q
ae
(to3ic) ,.*a*t.o
N3o-\+hnrn dbe\crnents
glmerrt r uto' u)on!&o
t
, /- 7>tr 7^>
rflsar.t Har\\eap Lh r n, varrrg)

f h:
h+t; qtue rrronb.b \l!l\c.reo'xQr, thgsize
Rfnl:Vafuo.
[:::1. i'uoqtA 'r,o ar \o5t Flqce

Il ",Ltmt o7 ,TT*
,to -{hc- Pe}ro^l volu.Q
pa,enr
= /"]
fc
,l attar , *o.f c.rlatel *he P
orrort volu.Q- r ura-
eof$Fo'Jlg olenL c\eroen ond.

(a[n*,or,,il < otd] )


r*- f s-q,
L.

rl
Faf^g
3

Jtebur

3
Horl {hg tt* &.)rca.'.+

o nt scteJlEeA "{he
Tou cron
gn ,
\0o ts node -t\ qb .rA c (,n &tt
"rttl-g

-) u}!- connoE Pfoced+ne


oJ*\-orls nodQ'

o1a 3 \ 56 18
5.22

c& {+,e p\acg crb fU rn. tooE No L &.tabeA r ond lr/ou uro-

co .nepJacg rhe Jsrb e\emenb[srtotua 'tro sv,a6*ed .b..u roob wodo,)

o 123 56
q5 l5 a5 rq 12 I

Size .rl \rle duco


ol n-l

he a P S
al .itt!-d 11"9 rulQ Fto
h e ch H a t
-> n &* delott'cn, check LoY J
eotrtPort e .rrill"
rsb
-> .Ioob
node .ui{n ilS
parenE ) chilJ'

s
s ) IO

isgreoEer
)
urhbh ooQ rPittt
loc"** "ode
zi 3\ tz]
cotoP crrr e fo lcs Tg\ rr
check r45 < lo o I r{5 61
!61 ilqaleY Fq,t^e
SertCgsrto ) soop (ror uS)

( "6;"r l?esurt )
:>

frol ,z)
l{5 ?3 Bs t9
l,.l c5 t{, to
ol 131t5 7
ot 2 3 r{5 6 +
s,

Heop soru _,r.tl.or9 Hoc P

l-> rorlt 5'g Hoic ke3 (l'lor f,eop )


b,
f-> o.*t. ++.e doto {rorr, H axWeg heop

Sur-Ib +he Horcheo P


r2.3 C

f rleop -> comP.tate bino.:y5 lrua-l


f
Lrsc-t *e {il1 ; -6. rs rrlhb
\ chech pdranl- !A $}teoteY +h on ehi

L, r.{ vo, Suraf ( pon

r-> No tr, r Ch€Ch Pq,ror.E > 6t o


L= 6 LJ e.5
U.t)

&

l5

E @ E! q
EIEE r5
o 3 2-
\
NeeE rS ( dllot'cnrr rt.+tod) 'e"ure*
ho.rr!
,9P
steP\:) +hir
qnd urhieh
eterneob rS
FlacQ hexe irsteqd
%lE, /rla pq-t-
Jo.r,F etemenb in
+q-a-o.
5. z\
steP 2
A t
I a,euted e.ternenb PJa{od herre
rsl ro l-rJ9 3o
ra3 t{ 5
Etr en eqch de,laHJ, $otturqrl& 63 chech Jdre 'l'vuo-

Sof^r{ied -|1,q Prope..t$ oyrenb ) child

l
s&eP3

@ xare &chil&en./ 20 chech otrb tr'hf ch onq


\ +
s*"f l5l ao ra3 JI a.b ex llanlon
( e'eil€)

A
3 t{
-) Coro Poxe l5) 1

1fi.rl-
(rvo c.lnnX e)

NerE
@ c.ro"^rA .trc dclote 2^droor
noile

SrePY 1.}g'n Jo$H-


rsEtooE nod€
l2 I q
r5 l !ol

chech ycurent ) ct'ri\

ho^rin .tL
3.)e,o&ax
Jcxl f,n( 3
S'rop( f 1rS)
,
a 3 I \ 5
q 9 f &o 3o I

trlecE 6S uuourA J,o d-etoLed


ltL 3\ s
1+ q r5 20

-r>
I -r eolas s*op C q, +)
5 .2s

q 1r 'a
I a
rS
trl 5
q
3o

Nelcb @ -u.rA &e- detrrcJ

r5 LO 3o
i
I 23 rt 5
NQxtO woLltd Jra- &l.l.te&

o f"
q rs
120
3o
r1 \5
t,g car\ ve&rce 459 ,ti6Q Com ,to

o(
") -> 3o
H eaf |te{hod.

I s6
Ffl 1,. *F"
3
U"tQt l'\etho
GN t-s
q eheck the Aegc en
in3 a^S chi\delrrrenbs
Y s5 ho^r r9o
+he Non^Jlot +Vti," ,L 'rls % t\or
-) ehQcJR
nool g dhiefi one hcr're 8o, to111 1 | Joo{ oods
.rn
I oyr
b"t S Hqrcheq P
h eaf *b{ HE .th od onlg
Lo:5er,& lnAet' 3 .
afP .TiQ d
,b."r n m & % h o d e.
-) Slort oPP\ Jchg
.to
Jltt {rrrn
go.rn \hroJ\, e:nal
he Ae&\od {trcnn
RfBh+ +o
3\nderc-
5.2.5

ure ojte hoving Jn'a{ nodo


lbut Jo {.rnd. our trrhtcrr '.rrrdelt-
,EJ(a!-
corn uale B,inolt
'
P roPet

J.co5 node- =E\ t \ Joon

n --?
["t"]+\-to+
La"Ett\ ton
B+t .kD + q5g *
qba -+ &"t nodlo \ l-1 1o 3o
oyte
ourl
Suppose r l! ruon'3- &o add one mo{q e\ernenb "h
binouS ,fsl&-l-
I

enro,roleA &ot nodQ


3
1
5
q "
[*'* b18
c [t/n]*t
* ' 5

L- .'o t
q+\
Sto I
oriSinel {oros

t23t+56"
i l-1 rol 3o
L

t{ q L 1
e
t Re\h S,larGQd tt* t/4, :r
le.
-
1.!D : 3
rmv€- to,rond, .t-atf {. dn"e.xornen-b
-
froce,lA)
ande* :1 .=; nlg
inAer =6 +(n1*-r)
5.*

r5
J rsEopv\5 $g
Y€at;t$ Retho+
1- 20
5 ) f.tqa.Y,esP
t0 P otents cornPo;^e urith ohild
t t ,) .r,$ nob ->Suo+
51 ponenE ) chi\ &
\
2o)ro ->No NeeAto€ry,sdof
S(trG?
f r">1oL) gar.te -)
\-\

L3 5g
1-1 lo

,rodeek'
ti .iP
a
.tI

M
1

"".d
T
) "n(te {
a

r-3 5(+
\
3o f
t.l*
t
.!--5

2o t1 0^o \ 5 lo \5
Lr.*i",3 *tsod ttt^"
eo.n 'H.orcrreo p
5 .2"

ill) AeJo.te *he doto'


Ncrt, qao(r tr' onL
eh.menE .un$ !l Aq$Ie d.
\hg toot
"Jr"Orll
rt-3 {5 6 1
I
3o 11 ao 6 \o \5
eSJr q\emen
E
.fra +hq JonS
, 3 Toot g\ernenE u.rourd.

"l
t{

t, Stcp I
.!,1 .}obts
,^:o Y c\ Ahe a
"t ta \5 6 ptaeod
\5 rt ao \ Illro iao l oh
fta}f

lo

A{tu eoch de.le$crn I


eheef. ha
r.}uco- So!.ia.tt *hQ. Harheap
StePz
r'1
15 houo- I cv'ildnen tuhicln o4q
-1
&o 3 lor,lU
rvor! t\5S
ull\ th lfa*-\eo'P' &o ..rt eompostcd tui+t1
3o!dt1 l5
E 15> 2-o
d Fqlre
sritr
seeP3

t2 5 C S.tQPtl
r 2 3 qs 6+
1 5 P"l3q
@r-
S,Iep9
t2-3\ e6
I

Moc eq oPQrtg
a eheeR &'ls-a- s'Ea)'*ti{ 'the

\ L3\ Eg'1

S.reP 6

t 13 56
pt

sbep'i- 8
Sd,EP
t1 3 s6:l t 13 56
t
I I
5 lo \s l-t 20 3o
I

S.rep q
\13 5 b+
€e
oode
-"H".,, f,t:;:"$e.c db r.dr'
-,,@b

f .t
G,r Iongo,t cv.J A
,$.ndl our &$
G.t. ,. t&-x.i) l .dr\nA otrr
righJC cV,lt\I
dnb Y (sx;1t t ' ->
uhl[e ( J-( n dd a1,q ., nf.wtXeeul) checre{he
r^rith
---- nod€
rooc
t ,utt sh,\I
.\arSest =!'
3
r.,rhi\e ( illn td nfrJ ) qLJonB esJ)
checx. {hq
f rtorrXeeb ;,;t chi\d' uri+r roor
no/e
= ut )
t
*6(.r*,$obI=i)
f
sro{ ef.sngu.tl , nf i:) '
l"\ox Leopi\,.5 ( c
r. , .ronXel*) )
1I
J3
heop sob Lo ,
")
f
{or ['i =nla. ', i>=r ; J-- ) t\o* heoP
-) GC'IA'
f
Hoq1 heot *b{ R,n,i) '
!
tc.rL;=nl i)zf ;u:--)
f
sr.n t stq r s(if) '

xaxheopit5 [ t,n, i) ',

v
(9
Heop *61 :->
_....-

H.o! *.bt ; +he ptoce'rA cr[ creolrnt o.t"* U: a &r crcltrx

.[rom o b.inostX 'lnee rte!flebcn&ol€A "*Gg


or\ qrro t'
$in-Heap to'{) l"\a'c-heoP
- fJc ..j.l '^r.xed.. r c'rQots€
no de-
a.rde:c c6 noo-s"{
- 3&65\b -\-rocn +Vte turos'

.i=o r la-\ l
r5-,
- He"?"tt \rI),, e! Jrecurl EiOn e

Heol Soru h\ 3ori-thro


qYT dP S,-h(uctutre
Fi{S,l ccrf\vQrt
roots nod€
h a?"t{ aa, otlg b e
"*"i'rq +he Ia'rE node
uri'rh
6b tne -heaP feP Ja
in the d+h {tre rooE d +he heof '
.r"n&,.r1
g\ze cr6 heoP .^-r t$eaj'eH thort
R.epeor +h oc

Steb .r . ecrmFJo.t€. B.inortl .\.',teg '\toro +hg a'rrori$

SEeP 2: -frans-bcrrm i'IIo rna:cheaP


mGr'-heo-p 'l
-ro Jra^sto{ro ohQnP &;to
po:.cn,l node strour&
olroolp &!- $&tgGlen
noil9S
+ho^ (o't) Q1\rr.ol, 'lo +he cV'i\A'

SolE
s&Q? 3 '. PetScrrrn \eaP
L> R"r*ug -lt,e rnaccimuro e .!em ol.t rrir eoen eLeP
t \.e r..o.re i!an *bD *he e^d Postuor\ a^d'rgmo e
dlJl
reroqin.inl S o.n d. ,franx{ottrt
elcmanJ< ib
.rh,f" o. maL-hea.+.
@
sal;t$ the
RePeot the sceP trnGirt *c.
$,!epq:iR#..
!,{oPai&LJ of h"o.? sotE

gort:->
TmP\emeo&ofion cL He"f

l# inc\ude{s'r&o'h)
g,rof 'lhe Posiltcrrr
volA 8 -"p (o.ru tq, jnb *b ) Func.ticrrt &o
er6 ,trl}o e\emQntsS
f
inU ,remp = *o;
rtq = *b]
*b :,femP ,
j
gu,lb&.neg toodea
a ur*h nod9 ..r
l1 ro r,ea1.r$5
cn i,t orr 'tnde:c in err CJ
ll ,rlY,f

It n + st"e 06 hea?
i)
Voi& heop\3 (;.r **cf , & Nt "n*
fl
j6t Jontes&: i'.,
torqe)i {bar.'
.rirt .ntr{, -(a"i)* t; 1t gt" ch\\& "D Y "r ooE
;5 righ t. = [s-x.i)+l) a''
_\
quc < N d$ ou['r qu.l >
**[ 'ranXusJJ
"t( -t
{j,}'riSr chilA
-fouqertf: ,!eur
,Iangen +ho
a,13e.ab
> a:ufrar3 e^bf ) "t
*s( ri3vrt 1N 66 a;utf'ri ShEl
JorrXes.i '= "t1'3t\ E.,

*t (**xer!)= i)
f g*^e(d ar,rt.if , do:'n('ra'3t"E]) ',

h""+*t5[ arun 1 N, .r-an3e^te) ;


\1
5.33

orurL)' *t N)
Voio\ heopsorts[G'r
I
i--)
{or(.i* .i =Nl&-r I i):
o)
heap*51(-ut,ru,i)',
i
{or ( ri,f .i = N-\ ', i): o', --)
f
( doilLof , dorr:r t"if);
S.rro?
heop.i\g Lor,rl , .ir j
")
i3
void. PriniQtros [ .r,t oxnt1, i"t N)
j++)
15*,,-* 'i:o; i<N" h

|rint€ [
pr intg ["\nx
3
.rh.E c)
f !o 3
,J{$. f1 1t01

.in,.E si (--) /sizecr( 1o:u1o:)


j
h o.rul gN );
',)
F {l ,l { [. s*.r"&' *".% i"\n
PrinbFrrartrLoln r N) )

1
oulPu-E

Sor,!eJ" qfiotJ u).

l5Iol5t1 zo 3o
5.3q

Mer Sorb
@
t1 gsorb fsor.tin3
rlechnique]
*) div;de - conqt^qr Eechnigue
-> divfde 4hg gug\igE ep &o one e\emenb ond mersq
SorLed sq$tisE .

t3b SucP
-) divrding +he tiSE inbo strbtisE
2"d stseF-) HeySinS og +hosg sublGb to one comp\eLq SorEed.
risb -

o\ 23 q5
r5 5 2q
6 1B
8 I 3 l6 ,:_110
cq eEmenb) -) Sorb ib in or, cen Jjn
lst- step -; div,dg .thr\ .lirE &e9
egt.ol
FJnd oub Hfd o'c}

c
"trat,
5 9.q
r6
20
2 3
C'l:
t-S&p
o+ 5 61 8
v& 3 IL lo 20
= t'5
Dividg o\% . 8
\ 3 lg lo 20

l6 E 8 3 t5 to b

5
*.* gubrist iS hevtn3 o^tj oyre element:

a I g or i +h m a &,,.. & d,) _ r-11,""I3, uo,r.r.d Cs&oil i n JQrt


f '(<rlendindet
('
,^eY3esorE( Ar lbruP )

t
.16 (-ou<up)

t
5'38
mid:(lu+ud7* ,B lb ub
me'g esorb( i
N o,8)
mclgesorE (e ,lb, 'nid)'
mer$eEoYE( f,r mid+ r, rrb)

f,nerte ( n r,rb I rnid,ub) .

3
;
r
rnerge ( o , ,{)
Sat t
roergesort(5,9)
--rl,rnerge(o,

:1,
\ , g)

J -l hs t-s-{) arts (r,e) rle r4e


(E,6,a\

-tu,") t{3ru) rner 9 e(o,&-ru) \


J I
-s(l,s) ms(qr q) a.rS( 3r 3 q
I )
rn9(o,) rns({r) hergq(or',:)
-/

I
m9(o,o) hs(r,,) rnPrge (o,o ,')

r.'s( oI 8)
\ ,
q(firl r-L
J .s(
\y
err) ,r.S(1'?,8 )
.ns ( s, S) ms ( tr6)
^ ,,x? ( f,r fr(

66,q rns(r,<)
5 ros ( t,' )
rrrSto ro)
e to cner$ E
con ,ho\ $
5llB n9(2ra) ) mer3 e(U,2) > -s(t,u )
rrevi€ (o 1o1r )
ldw I :q
m3(3r3 )
J
r5 2q a rb lo 20
r5 5 2t{ I I 3 lt, lo 10

\5 I ro 20

2q I
t6 3

I 8
3<L t5 rq
r5 s IL ,6
2o
5(a\ 5l r5 EE
trl t6
)
-trtta. I
s,l u{f
@ 2o
to
Zo
r6

s<8 I
u)\ichohe-
I8 rs 2\ 5
l\n
-tt
lF:- r.a

r3( tB i9\erb 3l E I l.r


LO

&r('8r
t/f I ao
tk
@
t5 2l{ 8 I 3 [ tu- to 20

r5 S 2cl I \ 3 rt lo 2o
I

lo 20
t5 tEl' J

2\ 8 \

j,t
I
rr,/.+ ; -6
16
os.it
Itna.uc
r5 E c -*\"/

5 cornp 2q lo
\rhich
ong zq r5 2\ ,r r6
\ 5 comP ,0
is t a r5 2l
P
.F;{ ji I6 corrp 20

\
5 8
r5

r"rge(n
f: ol7. 3 q s618
2tl
u' -[b; E. 2o
I B
i=mid *r )
kr -cb )
uhi\e [.i< =miAdSj<=ub )
d 1iy-rr) [
1cornp3
8"i6q
oc.: (: acjl) $ comp 3
@hile I j < = t^b
5 comp lO
P
oC'il ; I eomP lO t .,
btFlt = ggrcl: aCj
ti++, l5 (orlP tro
,,j
t5 co?nP irt; t<t
1 '6
e\se zq co.p16 e!e
fbtKJ --
_. l
qLJ 2\ cot\P 20 fl
t;rtil€ t.i -id)
)
j++', Y ?r''a;ni
nJ
f
b1x3: a il
)
k++'
3 l(++'
t
6.3+

Ho"3e.8oru
conluen olSorithrn '
Her3e Sovu ig a oiv(& orrd.
-> of .h$o ho,lve,r, .ort bsd6
+he ot'
- T,F d.ivideg "'lPut
mer$e8 +he
t$( +he ,llro hoNes , and tlnen 'i:b
&(})o 80r&ed ho\veg'

Alqot i4hro

f'ilL*
:::l:,:ff.:: n\erte -['urrc&fan"*''rvishts 1rnid' Voxiobre

S,beP 3 '. Prx$orm '

mcateso'tts (orrog , rq* 1 ri3hb )


*b ( re$; tiShu)
{eEurn
rniA t .rqrt +'ri3Y,u) /g
=
rnetXesoru( a'(rorJ ,Jr{3" , -id) -
rt'eYtqsorL( o,'*g r mid+r, vi3hEl
merXQ(o'rog, \.{f rn;{.,riXhu)
,
s,tse? q I S,top *he progrorn
HerSe8ort (-a"rr t,) ,.1 r'{ )
:$ 1>,c
, F.rr,& tb.e middlePo'rn& ss d,vide +he ql|,tqt d-n.Eo

Eu,o hqJrreg :

r midA\9 m -s+.(/ &,

' calt rnef,tesorE


t* t"ri"b hotb:
. cutl mertXgsott ( o:u,l1rn)
' eql[ rnertesorE .$w ,.rec*,d. hqf6'.
. CoU mer(geso..e(osu, 1 rn+r 1 r)
. HeYte *he .,trr:o ho}^le), 8or&eds .rh eEepz o.,o( 3.'
r collrnef3e ( evv,& rrr)r Y )
5. 3B

T Tqrn

..Jd
lf inc\rrdetsbdfo'h>
a'(l atJ'
.flrq .,..iJi
lf inclqde (conio'h) PtS"" a
ro | 'rnt
rr'i$h.F",de:t
Y
, .rir t | ;inb
void mer8e(inu orutll .!- )

fintso,i,l<l
inc n':m-.Q
int n&:lr-rrr
r,I lli"x-?il:iTil,lt t'-'-'"'"'J .'.-,

"hu ufntl r
R[..r] ., ll creote ternp orrots
,l cops r)fie dalo. 10 &cmp *ot s L-f:tJ
.$<x [.r:o ', .i < .td ', it +)
Lf.i) = o:utf -o+il '
*.xCi=o ) i<"ai jt+)
rl
RtSl = ".r,x f rn+
I
=o) i=o I

t! hite t.i < 3 J <n

r.i l

L u l
LL

j
abe
I
ourfxl = RCj)'
j++ 1

3
k++ )
l Ltl , i{ +hsrQ- * *U
coP$ J(he ^remo.ll1dnl streroe11Jcs ctb
il
5. 3q
qrhite[!i<nl')
f
otutLt(l: LL\il ;
.i.+ + ',
t(++
l
3
/l coPb -tl'e vemor.,rh3 g\ements o'b RLl ,.r{ tturrs' ort9- an$
urhileCj< nr)
[email protected]
= RLSI;
j+r ,
t(+{ l
iI
void. mer3e Soru ( .rirr &rutl-1 ,i"&,';trr)
f
*{( l<r)
t,.,, *:9tCr-')'&)
rnerte SorbIa'r<t 3' tm)J
metXe SorE(ar*, m+r r Y),
rnettg ( ary .-[1 m 1.r),
,
3
j
voiA printfl^''r"X Ii''r nCl ' i'c size )
(c
t J\! &

t*t.r=o) i< aize ii++)


!rinb$ ["o/od.", 6i:) ]
e

Pilhb S( " \'t 't ) ]


3
5. \o

.,i,lc rno,inC)
f
inb atrfl = f ,",r, rr3 1516141
inE qr.r siz€ = sizec6C-,") [ Sizeo((exr1o:) .
)
p.rirr,tS(tr G.iven o:tro5.rl, \nt,) ]
F.*dry(+
"-,-size )
prinh. h.rr..qJ crrur , ',
I
menge8orE(arul r o, oururr_eiz€ -l ) .
priots g( " \n So'..be& ar"rot\ ,, \r,,,),
Prius ( or.lr , orur-eiz.e )
T eturn O',
3

ou.Lpu-ts

Griv en o:t:t
tz tl l3 6z
So .Ll
5
5. \l

sl
ctr
s {
I
T I
EI (tlC{ >{
c'
L:I s s
l, oJ d- qf
3dE n n
\J
n
t-) 9 ? C,
c\.

o
i"fl o o o o o o
=J,,
--t-----F-
o
eJ
I o,
,E
't
c cl
ct c c
I
co
oE
o)
o) ( g)
gr c{
! o c c Ic
o, u
c tsb U
o o
g
s !
(f,
+o o
o o \, A-
5
o o +
IJ
c
o
U p J

a)
7
E
g
,l
{,J
o
8
J!-
E
t' c ?O't c ?C) o,
3 c{
3 U 6) c.l cr) :<
E oi) o
4 c 9 3 o o ol
0 o
c c a
t
,<
U q,
o \-, c c cl
o o o o o
C"
d I
o E
c.l
E q) ,+
{
t t-
I Ed c c Gq) ,
!
F

-!)
d)
n
.-)
rl ? 9
d)
c
6r)
o o c
o
t
9
ll
d
ca
oJ
o o o o
c
o
c
o ?
6
,6,
>ol
to I t,
3
-r'1 I
c
c
) Io ) !
o
.b
o -tJ I ct t
a IJ
{ -b b 6:l
o)
4 g
q/ 6 { .a o
-o-
C
E
-s
-F
't q,i

1, (J
_9(
{ o
o b , \ J ci(J
U o
(,.) d 6 5 o \ :.
o
o/ OJ { al
€ a :
co
c
H & -+. {
F -t
5'tlL

B;oorl Aeareh
j S 61 sq h:lo
ot 2
\5 eq 63
5 l1 23 25
\tt
,rtr
.9
mid
L
,.!+'r
o
l $ \oo 1
f/
tl 5
dtrur.li mr

b B6
: Lo+q/s]
9/g = u'5

\q
ld'
ca>e r. ' lllr-,n:
t*= -;o16..
ea.LQa: ..-
J"s b ?r' t!' f+l
dtft? ovt€ l agmidJ
.1. \
c
t{ --) 3 cole ca,be3:
o doro)a. dl
doto = 5q 59: aful, Crn'
+
5 sq< sg:: 29 iro
qtql
5 5c) otq)
5 9<qE MO
5 1)zs €.h-qturqoO
(, ("
L go $gnE 5e) -o,toB
J 51
drtbidQx- a 5
e6rn FoJre <i3trt \nde:c l\
51
=c. trl
5g 6-3, N6
olgorithm 5q (6a B e-5
L) gq ss6tr

(a"ro 1 ofrnid' l)
.i^E B.in ou5Seoxchr ( o,.,, aoto)
f e.rr,e.r{
{ =rnid-\ )
.IzO r ^C= n-\ ,
eI:,e
r-,rhi\e ( O<*) ..9- =rnfd{ \
'

f )
mid. = \
!_, le$) : \etLur{\ -\
u{ [ doro == et"id"]) 3
rebu'\n mid' "
5.\3
i-\
B.i"roJ.t SeoYtch e\go'ri+hrn

sJEaY t" S'lo'(t' the P'ro$rornat'ro'g drorr,


-r+re .l^liQJt
+he' go"rred 'tuts
sLeP 2: $e! +he u.berl ond
'*'e^ dolc {r crm
: rher' gef .r,d ocr! r+€'
srQY a Bn&
{n'rtoL qo-bQ- .ro {
cct(fr ?one tjrgYt
SeoncYring
coi+h *hQ mi ddle
S&e?q:J cornPanQ +hQ e\ements

*t: midolra s\emQnb


SLeP 6 : -> =;':"1""\arnsnb --:". .-^:
mid indesc '
u)Q- r\e&ut\l *oe
*:. -tcl" etementr '
+hon
s&ePb'.+ E.Ise'6 x .'r> gsteolql TiShE h.,rt
*hen x corr onJ* ..1,g in lrtre
strLar"ro'$ ;; +tre rnideternenb
ri3ntr h^-tb '
so ure \:rQ'cr-^'r x!. *"
r".rrtr {ot *ln9
g.rePr €Jr"e x
( .o arno\le{ )
") .r%b e.\emenE'

C P{o1.(arfl

if inc\ude < St'dio'l> .rrrE "r .rit x


oxrtf 1 , 'riru J- )
inv binarttr Seotctr I int
f
*[ [ r1= r)
f
intr micL = "e#..-/ .-Q.+ (r -) I a ,
"6 [--fmid] == *)
'reburrn mid' ;
5 . t{\

5t tort'lgr'idl > 'c)


f , rr.id
al , rr x)-,
b.rn"ttt SeostcY: fonn
'( et uttfi
3
e\ge r cc)
* r
'
t b.b oxt Seqvrc\'r C
ct.lt I rnicl
"c
{etuJtn
j
'retcr'nfi -\ l

i,.rg rrroirt C )
f arrJr?[a,sr\r\o "^ot ,,
'rnE

i$E ,r - gizecf,
(orr) f eize('6
n-\ r
9c );

, : :;:' binonr58":.':
--
.t# G no$ PsrlAQnl
( g.esurJt = =-t) I Piinrrt"
'P"*g[,. E
i), P&LD.Qrt& a'E-
.r, or'ro'r5) '
o/od' t' "reeuLb )
.'.+e.^- ,
"
1g-ttrnn O',
)

oulPu-b
or j,ndex' 3
EJ-offtcntr 'A PreiSe,r"b
9't{5
Luiteon geostoYr

.r^ de6.rn'Q& o> o"3e1 .l.rsn'r;ru} -8eoro\n


-> L lineo'l Seoreh
olgoti*nro ' lltnror'r$h eqc'r etcrne ntr
ol one enA or'd goQrs
-u '[ot'n d I
- S&oxb e\emanJtr
desrred
o( o Jists *rrt.li*"gt*to\\ oooJjn'uo-b 'lil\'the cnd "-b +l"te
oJdr'crutixQ' tt't
doro esE' olSoti\*rcn '
Seorc\^SJ
- f& ; eq-t:ui-)'t
S\3ori\\ro
e\emsnE cr[ o.rr[, 1 cat d
+hq $tf rnoSE
C) s.r-"r {rcrrn
X .u^iJt' gach e\cr$enb -6
one b5 one sdrnPo}t9
a.(\ Cf, .
urlth or\ g\ements t Yetu.yt(.r the
It x m"tcYrQ^
inde* . g lemencB
rnoreyr wt+tr on$ Gt the
O at x d*oc>n'E
Y gtsutfr -\ '
b.{
I\J
OqTafo
(9G dio'rr)
ft ino\ude ints Nr int r-)
Linu o'r'( Ll r

inE seone\.
f
int o',
{or(.i=o ', j,(. st 'r 'i++)
.{ totrf'.tJ --= cc)
'feEu}(n rl'
'r eE u'lrr -\ '

\
5- q6

inc mqinc)
t
inE orlLJ = { zr3rrarlo,u,o}
inr rc = \o '
inE N -- size orbco{{) | sizec5 fax'rEol)'
inE .result = Seortchf anr , Nr*)
.r! no.r. PrQAenb ln
[reeult =-r) ? prirrrs(" e'trt'e"r .lr,
= o-E
P Sr-O-rtCn'=
orto.r5.' ) ! PrintsS( " GJslnenJs
,rr.dQ':n o/o J' , I ,yr-qj,,,rJ-b)',

{e.!unn o .,
3
OuEPU-b oE}n de{ 3
656,erF .$ PrL!))onis

o\a su \ro
3 u \o
*^\o
\ro
2 ==\O No sv'+b
qorn?oJ\e

a J \.\ \o tto
.i. CotnPorre, 3=:\o ruo shi'(b

\6 ho
Qoff}Polte q= 2\o No shi tb

+ cornpalte [tt-;f6( g r-3


L: "relrr:rn *he_ i nc[e*_ .

You might also like