| Drtoe-ano- Gnquer (246
> The ope parocligm insishes Gee steps va cx (evel ofthe Kennsion :
(i). Ciside te proton tne a number of subpodbioms tal ane emaler.trsarees of
the Same prctlem .
C0) Congyue the sabprobeme ty scting Hem recunsively:
Yh the eubprcbiom sizes ane ama enovgh,
nee [ Base Case],
Wi) Combine the sdrlins the subprblems nib the eclutisr.
fit sce them Y brute
ae the ond geinal -
pidlm. perm
ae is
2 Bo Pn <— Oiled nto
fret exch i tbe, “Severeal szbpobim
- Subpredelems ee Re K
» Cutt Mbstactn : ncvicualy ae es es get
Cine compen’ MeN
| Fie, EO= gO TR
4. IF sate (P)
Qriotel onto 2 Nbeketore in
= h Ketan 8 () sebprcbioms Lach ace chviety andl
3. ESE GaN Wass, Corbiding »
a } size (TY fos a
4. Divide Pinto omaltere instances fy, h,.--> lx lseb (esy) 4
5] Appl Pac (A), PAC lB), ---* vache).
6.| Rha Combine (o1c (2D, vac lB) ~- _o1e (Ps)
O Wige Sind: AS ste, 4 wer Adtate, wer daeplee Sety- /
bekge-seeT (4, 6te)
1. IF px
ne QE: [4] <— DIVE (00) = 6]
A MERGE-SORT (A, P. al
ee 2 ay | nice [ tO 2]
5. | MERGE (A,B 9,7 COMBINE ‘n) = 6 (ni
Cee esol
“tl)= 6 (Pr)MERGE (A,P.9)n) ar MERGE i a
snus gpl ecole
Mt
2 meKey
3. Let 1 (1: mt4] anf R [43 +1) im
4. PRi=16%
5. | te=Alere-) | p boat
es for [= 16™ extra face
z RI Alef] | = 6 (m+)
8. L [mtt)=00 58
aes
9. REmtt] = ap es by
ee | |
S ae
12. for K=P 6 te Aetuaty
3. | oF LO?
A + Tre conpshrity of Merge Sant abesn clepend om ip peomidatins
Gre Gnploys apy. £00), ed
ar("A)+ on), rs
hs Bastese | mage cate |bbetene
8 (ng) eae) Tad | 2b 4 )
Aiolang Space = Olnr or
Extn ein oom Slack space used
Ls SS Gia @ i, fom Recursine coll
Sk
y ee sD
+» Best Case ¢ Minimum ro. of Companisons =| [L/
When Al elements a < Faret eloront of K
LERE[E] Carre : lll
D ee nossfe Onparivens = [t= 4 Ale Le
+ Wavst Casa g Woxémson roof Grpartens = ee 7 BTR |
When /t]-1 elements in bst L < First element of R
t [xfs[sys] [z[afn [R ce ue Nan. conparcisons = (443-1) 6
» &- Way Mergin Ba errr aN
eee Sim epone Boter-upp approach)
yi PEPE] =| 2] FI | Re tere anray
ae) We NL MAL AZ hae
al fess: [3] [517] pea + Lack Gist contains me element.
saga, 2m in ha ts | geo omer
© | she: platen (a | er
atime.
|
a fis: [ef[s[a[s] [ze] 9+ Let Altin] be an annoy of r citinel umbels. Seg and ACT >A] ten
the pain Gp & cxted an énvetsion of A «
>»counr-mv (A, ”) 1) No. of onvorsions Ste corny Ae <2,58,64>
F
1. InNnv=O
A. for b=1.& 1-4 A i ci
3. fon faltitén
4.| wf (av7 AG)
on [ if (Nv = INvEL AL SAGl (ee)
Se So, fare is 1 inversion befocon (le)
31), @9, 61)
+ Gohl Gd.
Similanly, al all. Eniversions »
io oa i so (6d) ).
eee (du &
2
iy i Gand 20. ff mnlencectbons = & = maoff anverstons.
FO E—1 tated nurobests = OnVerslins
2 = Qt, 2.68), & 4)
Gut, Cine Complexity of the bule farce meted is th.
[n= 0 (n”)
+ We cam reluce Line complrity from or 6 8 (nbn) using DAC «
>A prachial biehlem of tnvensions :
+ Suppose! music site -tnies & mater your song prreferamces with others »
> Yeu yank n- ere
+ Masic site consults tafabare te Finck pays with simélan taste .
Ble [ale No. of inversions = 2
Me ETS EAB 2) 42)
yoal 3[4 [2 ]|* at
- Séniterisy Matne: No. of tnveraions behucen-
» Aypheations of Inventions ¢ teve ratings.
: aa
My tank: fa ea alepa!
wl
Dnvensions ante =
+ Voting Tory. [ x
Sensitivit Pe mnie “Ga
reankén le te9 7 Songs (Og ane avented J 67,
¢ |
+ KReok segregation ) fe meta-searching bed a>
te web. Rea ee —
5 Ninveng the *sntebrese an dueay. | oi) Mow Fon
Ctatonative Fitosing (esestn E-Crmence) .| ener! ans> Gonling inversions using DAG:
1 Divide: Separate Get into fev preses.
as. Same count. enversions on cach Half,
Sash cnvexsions cvberre aaa ane tn Aiffurent talves, arel-
~ netann sum of tree quantve
MERGE” ofonation tn MERGE-SORT (Asserve such, faff ts De
» soRr-Ab-counT (£) I
. tf (bat £ fas ove element) Ao
Hos
a net
Retr (0, L) | fe:
Dye vie. (Bie (Os AE == (NED)
(ay, 4) = ‘sorT-AWb-counT (2) ——> a (9
(qe BD = MERSE-AND- -counr (4,8) ——» 6 (™)
Retron: (trite aa » t)
1
2
Sr
Se, Fr
4. (Ky, A) = SORT-AND- cour (A) —> + (A) n= a(n
7 pete]
a
a(d= a (may om
a
6: os]
+ Counting iverson tty MERGE spon’ Aux Space
gy ee oe Vaal
aw
A Bla fe [4 [ali (2 1 [ie |7 [23] =] 6
& a e eal
L [2 le 7 iy a [4] a al eal: forte MERGE,
i i z
mal
fe] > 8G]. be"
# Inversions: ©) éb é Bi °| mae
Here ane (9- i711) mumbei
« When 2 is copied into L, aes ag] (“ 3y2), of inversions.
So, there are, Grp cnversions »
= (6-144) 6 tavontons, and so on. | oonsnst (a1, 00)
Tolal. inversions = (erarztarety) he from whore My
ig 3 .*
ese
2.9.89,09,09 (u,14), (119), (166), (18, 18),
(2, 19), (2,19). (w 19) (16,19) (1)+ Not Stable, 1.0, nelative onden of e
ELE, ead sort -
‘floms (3 rot preserved » ara
+ Not Adaptive he, tuurptt aoe doesrit improm.on
= Bespite ne slows evnstease, 9 (n°) nusmifg tne, Falekeon® & often te best
practical chore . fon sonting because it ts hemart y offsient_on the average
and the constant factore hictlen. the 8 (myn) 0:
-case i.e, (nbn a
ane quite small. | Mogge sort also Has arrenage cose 0 (nlpni) , butt conetants-
- Ficklem in. (non ane bigger tar tat ff quickecrt |
> Bruckcont ato tas the adveontoge f sorteng inplace. [Menge sont eses
O(n) aurilany pace -]- Cutetgort & turce as fastas Merge sont» GS
> Guiekeort also aonks well even tn Vsateal Nerrony envinonment amd wth Gadbing
slant Aolex, , endl index
% Clvide: Fantitim (weannarge) the array Wee dea fy empty)
eubarways Alp, 9-4] andl ABts-- 1.
Ale]
Ale—. 4-4] Algth ~~]
Lack chmenteef AIp,..-9-4] ”
© I Kar ten Al]=%-
ary wel | ke
ale[#]4 [2 [s [6 [4
I
2és with Ft |p {
itsetfand putin (9) 9 ABS ES SR ctl
eae | #
Pil i =
Le[s[zla]o ls ]é]4
B is ped om be !
lager pain
care amatter partition:
qe
Fare % ane sunppel i
ane. Satter portiim | 27 173
grees
% Use loap imvantant & prove-
-connectness Gf PARTITION .
Srittalreation: Befere the inp
Stands, aff the conctrtions of Gop-
tmvantant are satisfred, Becaus
1 ts the pivot ancl subannays -
ane empty.
+ Maintamanee ; While the lop
4s eurmiing., F Aly] < prot.
them Aj) and AlirL) axe
Se eof ard then. 6 ard,
ane OP Ai) IS
Pivot, then énewemserd only f°
* Leumination: When te bop
eu Ce cc
Vi Te
i[3 [8]7 5 [6/4
é UVES a
6/4
2
2
a
A and ¢ ane wapyel [9 ]3 [eles
p
2)
e
we
5/6/8
Caner
SOG Le
Quicksorr (4. 4-1) REC OR TE (a, ttn)
4 is placecl on-its connect position .
‘texminates, (7h, 2 Us ele
in A ane partitioned inte one
ff he thee cases —
Ale...1) Spret;
A [tr4,-- wt] > pivets
ard A [re] = pivot.© Analyse of Ouickeont ¢ loeforrrance tepende om whether the partitioning ls baked ot not.
« Assume au fp elements ane clistinet. Oe pete, ore ane better partitening.
aporitsms for cohen cluplisate Up ee bp GP eleven eg
+ “Let Trlr) = Wonst case nunning tena man 3
WE Te + Th-x-D+6(5)
fost case,
tees cach ene
Wisnst-lwe? + Sh ave sottel on pense oxtt.
> Foatitiom anouned enin. on ax element | ©
> One stle f partition. atuays et 70 ea.
0 elements are TU) = F0) + 7 (n-L) + O(n) 2 ie i > "6 (Be)-0te
Gorey | 8 (D+ Tl) + 06) 34 OF ZAR ~< rlosoty
Tl) = Tlf rAln) z . po +6(n8)
2 [T= 8 le) uh M6 a ae
‘Keen tree. DREN
best= Gee? (rn inkettion ony Thd= TOt Th Dre
Lach subs
meg ¢ spr are we ane Lu
ES 3 FS alas) a Sey Gf seed pel lorments ae Mestian.»/
> ef et = fs ateays pe A 2 [Hest ase] Kean Tbe
warts ler paw,
fay Ti holy
ne ee Cate eae atenenty, |
(Tose deesy » ae a Se i)
opaee L(n) = 20 ls) +00) [Lecry | iE
Ula) = L (ns) +6 Cr) [untucty] eke 8 on
Dohring, Ll) = 2(lr6)~« oe ln) +alr)
= 24 (%-1) tor)
‘ “LOD =B(nfpn) , [tec]
Wage ey eal ae 2 [As: Ke Gack
Pr Our ass: OF pl tp ee lp cers vee), a
Connect his we ackt nametrnieatiin Guieksont«
* As lo Les teKewcmivat Gurrenr) |: Aéetcase st 108: Lentitton around a narclm. clement . Eyed Time = Ofnlyn,
+ Kenneig fine & inokpenctent § te Op ip Gp eter. Mey sop practioly,
¥ No , ae newled & te mace about te oka
+ No pee fp elicits the éret-case teharioute.
ea & oletonmined by ony the pS: (oe ne
* IG han assumed in prrevious version,
RANDOMIZED = PARTITION (A, P “) that all? ip ap permutations are OE
t. t= RANDOM (Pn) | thy, which is rdf aluouys Crue , ca
Be exchange ALA] withAGD a ue A bay a
3. netuen partion (ARI) | Cea va
+ Kenolom! ey, the pivof element,
AN DOMIZED — QUICKSORT (A, Pte | ell ont rye cause the split of te
a $e reasonably well
ie aise
& 4g = RANDOMIZED “PARTITION (AP. ”) Se
3 i, ~QUICKSORT Ae 02 =)
4. RANDOMIZED QUICKSORT (Go ere tt)
» Aralis Let 1(n) = the nandom variable fon the numing tome f rancomiael-
quieksont on an fe Fvze n, » assaming nara muimbors cite ontyperctent..
for, = 4, ~~~ OL ofine te dncticatore nardom. variable .
Bec 1, Gf paarinan generates 2 Kirke be
0, otserwise.
E [XJ -4P 5X20} tL PAX ALP = OF LoL, sie all splits ane -
- epeally Chey, assuming clovents ate Aistenct .
Se ke +8 (n)- ad aint spit
@ ce mee! G 1: nx spleé
7 (mi) + rl 10). ff rt:0 sft
= ("8+ r6«d) +l
K=0® oy: Ba ee (slo + rlesci) + (9) |
= elec (rloeroedt0O)] (lomty f opatin)
Rae)
a = Ela. E [7t9)+ 10%) +09] eo Xe
choices »
ie = El] +E. € [ror] + #Z 00)
(se #4) ; Comey Peper)
HO] = 2c rtog 1 0l0) , (Summatne Bre stato)
The K-04 res fe absonbel cn the O(n) «
fase: £ [100] < anfyn fox cometint a0.
+ Choose a. lange enaygh se Bat ann. cominates E [rey] foe
Saffrientty age 7a 7 2.
Lee bits FE Ay < fovgn— fmm [bpife axeorce)
kag
Vero) Z Z ang + O(n) [Sebetietion method |
oR
hey | Substitute. inatuctrve
£ 42 (Lorn $0) 120) Aypotiess 7
= angn ~ (F e 3)
xpress 2s closed — pesitual .
K 22g
a & chosen. ‘S ersuph so Get Se ee a(n).
2) Kenctomtzalin ae sont St ae “ogy 2b ype f anny Soom cnssehg
oust case odes an altendly -soieted cautres *
wenst case behaniourt im Ten- oe antes ele, bat nid tn Kerobmizeol-
Giucekeoret--
a49 Keumaie Glts fr veg
SE ( A, re :
: aL
(lc em!
{rhs ae vg ae :
2
> Lon urcksont :
. lack Space -oflog.2) [Best Gace] 3 = a(n) [hiorst Gee [
A Ailing [Extra pace = 8(4) So, Guickeont & MANE: soxtong |
. p Space = O(n) best case lanstcat]
ei Pastis Gutekgont srt ainlaie eae schosip of tps incase d
alugticate keys. (Fa hee parrition proceline) , 50, NO.
>» Adaptive: Gutekeont is NOT adyptive, Because incase ff sondest Sp,
(ts penferemance ches ro improve. overe [ist e230 -
(in von A
» aa sont quas ferme Leip Hoare) t translate Keussian int English.
= But in “Hospes’ PARTITION proceclisre, Fast aren cline cece ese
HR CoA. pirat element rather ‘than seteting the (ast ckment as pivot [as im cL RS bid
Hoare [> ~~ RMT AT an ;
> Coy Howe gdh Categ Peal in £782. Vie
HOARE ~PARTITION Ape 7
4. x=AlP] 8.) ' REPEAT
Bre tee Pop
a prt a ie re
4. Wai TRE to, | UNTIL ALTZ
ie KeAT Pele cs
6 he ag ay | exchange ALI] with Alp]
| z- wnTite ALTE Ip ELSE RETURN |
pane SodusStage Metiphaton¢
> Majtheatin: Given too n-bit trtegers a and 6, compute axb.
Gpreot’ Sobel iay ratipbcation. pester +
6 (n*) BE operations
/* Mypere. Zane 6 le- | tol « Cajeclre [Felragorev £086] = Grad sebel
te omabe both inteers| pei
equal roof Hits of | ol ame © ets
Jol
ep) x= lal ,g=10l elon . iTiestees [ease 1960] = Conpectere
700011 és false.
Ten mit, gre
ee ___
Civile. amt Cong vere Maltipleation + | m= [7%]
a .
> Bmutiy too n-Bit sntegers x alg. | a= [mmf berm
+ Biviole % anet y inte Gw ant figheondete bs
, ;
+ Mattygly fore 0g, bid Infgars, necursived | kieye gyn
= Adel and shift & chain. nesilts.
my = 2" +6) (2" td) | Th. elt), tfnes
C atest 2” (ee tad)+ ba at (part),
if mL
4) Baarrone Holl Fa lO oogk a>
Here, m= Ae Gs: n=8) -fro= 66]
a= [ tece et J = 1900, = (000/10 moet 24 1101
C= 4 f)ttoeeol ra Chem eto cert mod 27> 000!
C= [wr Adee
Leftshift al
ty m bib,
So, x = 1000/ 1101 1{0] 000,
es Pa i i
sea tual
peer ase TD J fon decimal rusmbens, use bse
1. 1 (m=L) Return 27 10 instead of 2.
a. Ese ne ben Lio”
3 m=|Al | | ae a i
4, | a [%n] > b= xed 2” |. = [dfon],a-g7'?
5. cH ftpm] 2 aeagmaem | Ant So om. A
6.) @= muLtiPlyalare™) 5 f= mover iptyale, A, ™)
a He a:= mutipLy-oac(b,em) 5 fe = MoLTIPLy-bAc (a4)
Return. 272 + 2™ (qth) +f> Me
Keratiube hicks To muttily two n-bid integers warty :
Vvicle anol W int bw and tigh- - onclee bits.
> BD compute micttle teem (be pad) Use contig
berad= (ar) (erq)-ac-bd oy To
m= 1 74/ b acted - pd (0-4) (e-4)
a= [%Am], b= % md 2™ Jo bhewit we it.r/
c= /dAr, aay mda”
ays (2%ar$) (2% +4) og te Ee eg I
= Pact 2 (berad) tba
= ae + ar(arslerd) -ac-bd) + ba
o @ a
© KARATSUBA-MULTIPLY (24,7
KARATSUBA MULTIPLY (2492) pel, ifr
4. w(n=4) T= 1 ol), f 4
2) Relmm oy ma [ZT witato: 37(1%l) eee?
3. ELSE o As | i =)
Heal ca 20/ voc)
5. | a= [nfs B= xmetZ™ Ns (gy
| / «Gmc sbe = o(n")
WP he yd Be ”
6 c=] dfnl > a-¢ ar pales werataaka aporiton= of")
me | e = Kararsvea-morrirly (2, ¢, » = We x Harvey Véon-clow Hoeven:
a, | _f 2 KARATSUGA-MoLTIPLY (2.4.7) —> TW iS alngn) 2) [late 2009],
e qa: kagarsvea-muerirty ( Ja-é| » |e ject], ce) Satan i
wo | ( Rem aXe x ar(ert-g)tt fob
Flo sign Fp Gre
« Apypheations :
> Seeger:
© We semehimes need & cE -fvo Ca uarrbers Fo ~
Cy ty
‘ Fractiee. Bampies : @) x= 1000 , gem QD me 146128, Y= BER120.
oes