e@2
QUANTUM Series
Design and Analysis of Algorithmswww.askbooks.net
A.S.IK.
Always.Seek.Knowledge
All AKTU QUANTUMS are available
* An initiative to provide free ebooks to students.
* Hub of educational books.
Ul CR UCC em COE}
eC ae Cen er incom marcus
2. We don't intend to infringe any copyrighted material.
Pa ura cr ek uit
DCR Ar ee CROC RLS Loe
PWN hina nesi ee
PUBLISHED BY: ApramSingh
Quantum Publication:
(Unit of Quantum Page Prt. Lid)
Plot No, 5827, Site 4, Industral Arca, —<=<=<==C ONTENTS ===
Sahibbod, Ghazishnd-201 010, .
‘Phone: 0120-4160579 ROS-502,
Email: pagequantun(@gmallcom —Webite: www.quantumpage.ain
| Delhi ie : U650, East Rahtas Naga, Shabdara, Delhi-110092 ‘UNtra:iwTRODUCTION (0-48 01-998)
‘erie: Alyn Agri Coney of Neti.
(rand An
ot Algorithm
© AUR Resmi Shih ae Ques So Mere Sr Hsp at Campa
No pt of hie pba may be epoca tne fl soing Ago Song ier Tae
‘nay fom by any mons! poison
UNIT: ADVANCED DATA STRUCTURE. 2-1B.02-485)
Tufte conned i thre derve rm sources eS hh Ts B= Tes, anil Hep, Por ep Ts
baler ote lable Every eferthas teen made ensure Se
accurecy, however rier the publisher nor he authors UNIT GRAPH ALGORITHMS. 1803-408)
susrance he accuracy completes fay formation
published here and either th publisher othe shoes
shal be espnshie fr ny eros emis, or damages
sing out of wa of inrmaton
Divide and Conquer with Examples Such a Sorting, Mai
‘Muliplcaton, Cowes Ha and Searching
Greedy Methode with Examples Such ts Optimal Reliability
‘lication, Knapeac,Minunum Spanning Tes = Pn’ and
‘ruta Agontins Sigle Source Shot Pats» Di’
2rd Balnan eed Algo
Design and Analysis of Algorithm (CSAT : Sem-5) .
LUntr4 : DYNAMIC PROGRAMMING (18104-408)
1 Balin : 21041 Dynamic Progaming with Exemples Such as Koapeset Al
2 Bain : 20112 Rare fat - Wah and Po Anti Romane
3 Baiton : 2072-13 ‘Moan Pie
ected an ound with Exp Sah aveing
4 ition + 2079.24 Scien Pblem, Grph Cong n Ques Pedi Hanlon
88 Baition «201615, jen tnd Som 0 bes
8° Baition : 2015.16
et ee UstEs SELECTED ToPICs 1B.05,34m)
‘ ‘Algae Computation, Fst owe Taf Sing Mating
8% Baiton : 2017-18 ‘Theory of NP-Complwnesa,Approsination Algona
98 Bdition : 2018-19 (Phoroughly Revised Ealition) 4 Randomized Algor”
10° Eaton : 2010.20 ‘Shorr quesrions (sqb1080208)
Price: Rs. 120/- only SOLVED PAPERS 2013-1470 2018.19) (sP1Btesra2e)
‘Printed at: Mayank Erp De TOO:Introduction
(1-28 to 1-148)
A. Concept Outline Por D nnrne 1B
B. Lang and Medium Answer Type Questions vovncwnn BB
(1-148 to 1-808)
Comparison of Sorting Algorithns
Sorting in Lingor Tie
A. Concept Outline Par 2 nvr LAB
2B. Long ond Medium Ansier Type Questions seven -15B
LAB (CoHTSem-5)
| tactin Algor Arateg Rina Copley of
| Algorithns, Growth of Punetons, Performance Measurements.
CONCEPT OUTLINE : PART-1
Agri An algorithm sa sequence of computational teps
‘at easfor the input ine the outpes
Input» [Algo + Output
+ Complexity of algorithm + Complesity ofan algorithm is
Aenea ty two tera
1 Timecomplxty
& Specemmpeity
+ Analysis of algorithm The analysis an algorithm provides
‘me bs inferatin abst that grin ke time pace,
erormance oe
1 Analorthmis ast orale for crrying ut alelation ether by hand
‘roa machine,
2. Ieisa ine diepy-atep procedure toschiev required rsul
8. Itisasequence ofcomputationa steps that trasformtbeinputint the
tp
44 Analgrthm sa sequence of operation performed on data that hae to
‘organised in datastracturee
‘Characteristics of algorithm are +
1 Input and output: These characters equire tht an algrithen
‘rods oon or mors oupats and have sero or ore inputs that are
‘teraly sapped.Design and Analysis of Algoihne AMLCSIT-Sem.6)
2 Detinitences + Each operation must be perfectly clear on
‘ambiguous.
| Bsfectiveness: This requires ha each operation shouldbe fective,
{vend tepcan be done ly aperson sing panel and paper inate
sount ef tine
44 Termination : This charstvieti equres that an algorithm must
terminate after ite momber af operations.
‘Gaeta ] What do you mean by analysis or complexity ofa
algorithm ? Give its types and cates
Tarwer
Analysiomplesity of an algorithm +
‘Tee complet ofan algorithm i function gn) tat gives the upper
‘ound ofthe sume af operation Cr Tuning tie) performed By an
slgorithn when the input ane
‘Type ofcomplesity:
1. Space compleity:The pacecomplesty ofan algorithms the amount
ef memory ened trun to completion.
2 Timecomplesity:The tine complet fan algorithms the amount
‘dine teed toruntoconpltcn,
Cases ofcomplesity:
1 Worst case complexity: The runing tine or any given ise apt
wllbelowes than the uper bound expt possibly for some values of
‘etepat where the marimum i teched,
Average case compleity‘The running tie foray given sie inpot
rib the average suse f operations over al problem instances
Gragrenaie.
Beat ease compleity The best case complesity of the sort ie
the faction dened by the minimum numberof tps len on 80
[nstane of
‘QueTS.] What do you understand by asymptotic notations ?
Describe important types of arymptotic notations.
Discuss aeymptotie notations in bret. [RRFUSOTETS MGS]
1. Aymptte notation ia shorthand way to represent stat possible
‘d'soest pase’ running time fran a
ewe leorith, ing high and
introuction
14m (CHT Sent) ——
ae a aot n wt tin
8 Tre umadtofind complete af functions.
setts nd spl om
iphoto aan er
oe tina tintin ei
tant 24
Terao thr i vie etn 28
8 Madani afte vn fn vase temo
Clg and et ils
and
ess)
0)
se)
saitowa)
ig. 135.
2 Notation Upper bound):
‘2 Big-h ie formed method f expressing the upper bound of an
ort ranaingtine-
1h This the measure ofthe longest ameunt of time it could possibly
{efor the algorithm te complete
More formally fr non-negative fenton, (and gn) i there
(Sean intger ng aad sonotant¢> Ouch that fr allintepers
fo sea)
A Then, f(a big oh of). Thine denoted as:
Tie Out)
the at of fanctions which, asm gets large row fster than constant
the fi
* fin)= Oe
ign.)Design and Analysis of Algorithns LED(CSITSem.5)
& Notation Lower bound
‘8 Thisnoaton gives alower bound fora function within a constany
factor
|b Weverite in)» tn) ther are postive costantsnande sch
‘ht tothe ightofn the vloe off) aleayslies on above ce)
a)
cn)
*ay=m.4)
(ners)
|. Little motation () tis used to denote an upper bound tht i
‘sympa ight because upper bound provided by O-nottin is
ott.
ain) = tn) foray postive constante> 0, aconstantn,>Osuck
that 0s fim
0,fconstast iO such
tO caste) Lar contents
the number fsb problems in the recursion
‘Ub he portion ofthe orig problem epresonted by each
subprotens
fy Tan pen etre
Ej
‘he ene ltt fen
tue
eet
poet teb0
te i:
Twists
amie: Tweet) +08
the given formula the variables ot he allowing vas
Sines this equation ds, the first case ofthe masters theorem appli tho|
sven eerrence relate, tus reeling soation
Tu) =O) = ei)
case2:
Irie tre that:
fin) =@tnhn)
eftows tat
Tin) = tm
ae (gin)
rw ut2) +n
Inthe ig he ig ae
= 2b 2,fln)=1, loge =log=1
Brent a
rouction
1-9 (cat Som :
maeanaen 7
Sint patent tod cae he ase tere ai
‘Relive curene relatos, thas esuting sl
—c Thay in og tn log
fir) = nse“) for B> 0
ndifit letra
itor (2) se ora anal rata eee
iat
Bw =a)
ot rwait(S)o0
ve genieseshvaea get te tag ate
7 oa, b= 2 fin) =n, loge = log, 2= 1
ee
xno)
)
fi) = 24n!*1) = 0008
Since th equation tlds thir nse of Master hoorem is apiied
Now, we have te check for the second condition of third ease, fi roe
that
0, fllows that:
Tn) 017)
we inert once more the necesay value, we gt
Tw) cow)
Thuy th given reeuene lates Tn wasn (0.
BE] Te recurrence in)= 7a) +n* describe the running
{me of an algorithm A. A competing algorithm A has a running.
time P(n)= a (nd) ni What isthe largest integer value for nie
erm tater han?nw 77(2) +08 an
Tide or (ent 213
Here, equation (17.1 dines the runsing tine for algorithm A and equating,
(17.3) defies the running tine fr algorithm A. Then for finding vale of
forvwhich A's seympteial faster thanA we find asymptotic tation or
‘the rearrene ty ting Masters method.
[Now, compare eustion (171) by Ni
1{4) 0,thennl=n. (nD!
Here thea cti or arbitraryn idee in terms soley
‘allen whichis er tthe base value.
‘Observe that thisdeiton ofa irecursive, since itrefersto itself when,
swe =D
{Wee Whats recursion tee? Deseribe,
newer
1L Recursion tre it pstoral epresntation of an iteration method,
‘phshisinthe frm ofatres, whereat each level nedesare expanded
2 erecuson teach ode representsthe cst of single ubproblen.
‘8 Recursion trees ar partialarywsful when theecarrence describes
the rmning tie of did and conquer aorta.
4 Arcuri tee ie est edt generat a god guess, which is then
‘eiGedby the substitution method.
|& leeemuthd tosaloe the complet of algorithm by dingramming
thereurie neti cali.
‘This method canbe eee
oe Solve the recurrence:
Tind=Tn-1)+Tin-2) +1, when TO) = 0 and
Tay=1.
aver
Tin) =Te-0) + Tn-2) 01
ArH esl, TU) willing to
wea ache
bene
FP ete
sPeteae es
: a ae
oN Ly aoe
[ssmern terme oti reese
-e-l-12009
Solve the following recurrences:
Tin) = Tin) + Teal) + Ti) +m
a
Ne
/
aceite) yaa) taite) 2
ANAK ANNAN INININ
tag ines
cor
= a(rloga
[QGETTE Consider the recurrences
Tia) «30 (aa) sen,
Tin) = SF (ni) +n where ¢ is constant and n isthe number of
Inpots, Find the atymptotie bounds.Design and Analysis of Alorthans
wer]
tm) = 302) een
vrecan dren eae te fr 21
Ta)=nenent on
Ta) = AUeogm)
logy ties
oa ™
= 7S No
nN JNINININININININ
Tin) «nen n ont logg times
| Ta) =0inlg)
y Tw) = 6r(2) +0 sy
Comin ein tothe (2), ee
| ok
fn) =nt
Spl end = 9360
) ‘Now aply case of Master theorem:
find ® cx) ayers
= Aint 884) = ofa) whore B= Gor
Hience caso of Masta’ theorem eatin
Tin) =f)
Tin) = nt)
REETIE] sotve the fotowing by recursion tree method
i Tin) = n+ To) Tenis)
Es —————__
Introduction
| utng end Order Sttne: Sha Sr, Quick Sor,
"Merge Sort, Heop Sort, Comparison of Sorting i
© Salgorichn, Sorting in Linear Time.
CONCEPT OUTLINE : PART-2
‘+ Shell Sort: Ttis a gorithm that roughly sort the data fst
tnd move large elements twards one end end smaller ones
toward the ober
Complesity: Oe)
‘+ Heap Sort «The heap isan array that can be viewed a8 &
complete inary tre. The tre filled on ll lve except te
Towest
Complesity O(n og m)
+ Merge Sort: Iworks on divide and conquer approach Set it
Aividesaistinta wo sublis and sort and ten combine ae 8
ow sorted ne list
Complonty:Olnloen)
* Quick Sort: I mors en the principle ofdivde and conquer. It
‘orks by partioning sven eras.
Conleity: Oi)
(Questions Answers
[ioe Aner yp ened Anever Type Qestions
N‘Que TE. ] Explain shet sort with example:
Aaswer |
1. Sheleortisabighy ficient sorting api ands bated onnserin
sort algoriti nd wecancoe Fess.
2 rough sorts the data fist, moving args lees vars one ng
‘ed sal elements towards the te.
4. Tnshelsort several pases ove the datas performed, at iae thay
hola
‘Aer hel pay, the data ify etd
.Thesel srt dor ot rt the dats iteel, tineresss the effin of
othersurting sgt.
Algorithm:
Input: Anaraycflogth n with ara ements numbered Oto ~1,
Lee round)
2 hie ine>0
2 Briwincton-1
temp al
“leis ine > temp
aio te
jejnine
oneuap
4. Sane round 22)
Forezample:
5] 86] 75] 20] 05] 90] 60] 65] 90] 50] 10] 75] 65
‘Thedistancs between the clement te amped is 8. The ub generated
vvith the ditane of ara fallow:
Subsler al) al) alo) al) allt)
Subéle2 alt] ala) al] ato}
Subfles a} al6)— lB) alld)
Toputto pas 1 with stance «3
(LD | [oo Tes Too To oe [es
T I T rf
@
(Output of pas isinput to pass 2 and tance «2
1-108 (COT-em-6)
troduction
w]e] 90] «50m [oa] v6 [75 [| 65 [oo [05
COvtpotof pas 2is input to pass and distance = 1
fj sno ea saa ase Sts et
w
{FOC EEE:
Las] r5] 90785]
oy
Outpetofpass3
mallee
foriejsiton
AU Oand Ali) > key
dealt eld
feta
AA CheyDesign and Analysiof Algorithms ATI CSTS em
QueTAT | Write non-deterministc algorithm fr sortinis
ae |
‘Non-detersstialgpritums are sgerit that, ven fr the same inp
‘eves diferent ehaviours on diflerent rune, erations and executing,
‘ea «Othe atare
Bi = At)
endo
fife 1ton—1d0
ify <1) een fatore
sealer
ran)
1, roe
‘Que AE | Bopain che concepts of quick sort method and analy
=_—_———
1-180 (CSITSem-) Introduction
& doirauiss
6 tenietel :
then exchange Al) © AUT
1 exchange All Here
return +1
‘Example : This exanple shows that how “Piva” and "Quick sort” work
apps eas 8, 9.2.6 5.3.5.8, 9)
Sor the array A sing quik sort algorithm,
Solution; Given array tobe sorted
step Thea Peed ob si lees, Pet P=
EEE
ell
P
‘Step 2 Find first element larger then Poot (ake underline) and find
‘State oo lage han pivot rom end makeover ln.
es
Atscomplesity with watable example. [ARTOOTEI Maris]
Tarver
Quick sort:
(ui sort works by prttining a given array Alp «nto two nonempty
‘barry Alp -g~ land fg 2-n7] euch tat every key inAlp gil
Tnsthano gil to every ky ing +1 Then the two subarays are
Derted by ecrcrecall gue oor,
Quek Soet Apr)
A lperthen
2 qe-Partion(A,p.7)
3 Recursive call ta Quick Sort Ap. @=1)
4 Recursive clo Quik Sart Aq 1,7)
Asafirt ste, Quick ort hace ae pvton thet inthe arry tobe
‘arte, Thon aay ie partitioned on ether ideo the pvt. Bement ht
sles than or exalt pivot wlmove tard the lft lerments that
seater than eas tie wl move toward the ight
Partition A,p,)
Lb seabl
2 top
3. forjeptor-1
P 3 +
—— ont
oc
ove
‘The pootershave crosed
“averting on lft underlined1-208 (CSTT-Sem-)
Introduction
DEER:
P
Now, voting process omplete.
‘Step 4 :Reeursivly sort ubarrays on each side of Pivot,
Subarray 1: [271 ]3]1]
Subarray 2: [9]5 [6 [5 [7 [5[8]9]
Fist apply Quik ort foreubaray 1
Bes
Usdelne
?
Underline
‘The poinershave crossed,
‘ce, ovelne nef underied.
Sea pvt wth overine
‘Now, forsubarray2we spp
i
=
on
EDL
P +
a
HELE
‘The pointer has rosea, Then swap Prot
Leelee
Scbaray 3
Bs Tel] «51
i
Fi touah
P
‘Swap ovrline with Pivot,
(ele
Oreline on ef of unerined
‘Sap Poot wth overlie,
‘Now combine alee sobarrays
«EE |e
Sorted array
I
Piet
‘Analysis of complesity
Worst case:
et Tn) be the mort at time fr quick orton inp sien We have
Ta) = max, Tg) + Ta-g=19 #(n) L382
here qranges fom Oton~1,sine the partion praduces two eas,
tech ving ste n= 1
Now we assume that Tn) 0 aloo
se an pick‘ and’ large enough 20 that m log m+ b> TU). Then for
nS i wekave
Tn) 2**0,,.2nlohlogh+)+ 009)
= Bah, blog h~ WBC) + 2
(-De0n . 18)
Atthis point we are ain hat,
0, Hogs 12 lg n U8t0)
‘Substituting this claim inthe equation (1.18.4), we get
Ta ae Ue ogn i 2 bin—1) tn)
Sanlogn-anlts 2 in) 183)
1st 18, 9) ad al tis and ween
choos large enough so that al dominates tn) 8,
‘Weeoncludo that QUICKSORTs average runing time i 6 og n).
[RERTIOT] Discuss the best cate and worst case complesities of
guk sort algorithm in deta,
Inteodution
1-221 (C8ITSem-0)
1 The Bet thing that cold happen in quick srt woud be that each
pattoning stage divides the srayextyin hale.
2. mother word, tho best tobe a median ofthe kee in Alp. every
time procedure Partition sealed,
{8 Theprocedure Partito’ always split the array tobe sorted int two
cequalsted array
4 Ite procedure Parton predces two regions of sien then the
recurrence relation
Tia) s Ti) + T+ tn) < 27) + tn)
‘And rom case (2) of master theorem
Tin)» nlogm
Worst case ReferQ.118, Page 1-17B, Unit
‘GeeTBO,] expan the concept of merge sort with example.
"Merge sorts asorting algorithm that usesthe des of divide and conquer.
‘hislgrithm divide the rrayint two halves, ot them soparately
snd then merge them.
8 Tis procedures recursive, with the base eiteria hat the numberof
ements ia the array tnt ore ha
MERGE-SORT A, 9,0)
MERGE-SORT Ag +1;2)
MERGE 2.4.7)
eee pe tere
:
:
:
2
(Createarape
RiLongel
fori=ttom,
y+tlana
&
Lineapsi-aer
Design and Analysis of Algorithms
endfor
5. forj=ttony
mn
RUl=Algea
or
Hin + l= Rng
ae
uasRy
AU)
reeey
ele AW=RU
fair
endfor
& et
Example:
10, 25,16, 5,35, 48,8
1 Diente aes
2 Considerthefirstpart:10, 25, 16, 5 againdivdeinto wom
says
Ws wg
a} Bx}
‘% Consider the second half: 95, 45, 6 again divide into to subarraye
8 6
Ba 5
esa]
4 Merge these tra sorted sub-area,
a4 C9 Som
"Thisithesorted array.
‘Gee ] Determine the best case time complexity of merge sort
algorithm.
=a
4 Thebes ease of merge srt oesurs when the largest lement of ne
xray is smaler than any element inthe othe ary.
2 Forthiscaeonly 2 comparisons of array elesnents are made
8 Mg sort comarons are band hy the rearens eto of
‘therecursive calls used in mer
4 Asia the ry inte aoe een eon eed
Iotrodstion
rove r(S)e1(2)s0-27(—)+2 stn
5 By sing arable # to lndleste depth of the recursion, reget
r= 0(8) oan lane
Forth bust case there arealyn/2comparisns hence equation (121.2)
canbe wsten at
ro xa(s)oah
1. Athott level oes tse
Ben
‘tone
4 Sotherecurec faction df
Twa e7( 52) Bogan
same 5
Bm) =O
ec heb en cope meat tno
‘ReeTB] eoplainnoup srt algorithm with its analysis
oR
SennenDesign and Analysis ot gorithms
‘Whatis the running
is already sorted in
T-25B (CST Sm,
time ot heap sort on an aay A of length nth
increasing order ? "
‘on
Discuss the complesity of Max-hespify and Build Max tte,
one
al
Peeters naiue aus
the second angst items found and this proces isrepested for allots
clements
2 The general approach afheap cote a fellows
‘From thegivenarray, bud the inal max heap,
‘Interchange theroot (maximum) clement withthe ast element,
‘© Use repetitive downward operation fom rect node to rebuild ho
‘heap ofsie one las than the starting
4 Repeat stepa and util there ae no more eessents,
Analysis of heap sort:
‘Complexity ofheap sor forall cassis Otn logy).
‘MAK HEAPIFY 4,0):
eight
itt seap-sie (A) and AUT > Al)
then ingest 1
ls largest =
\trshesp-ice [A and Al >A largest
hen anger
‘flag’
‘then exchange Al] ¢ANargest
10. MAKHEAPIFY largest
pena nee
2 fori etength A dom to |
8 doexchange Alt «A
“ heapsize Al «heapsige A]—1
5 MAKHEAPIFY 1,2)
‘BUILD.MAX-HEAP (4)
1. heapsize CA) «length Ad
2 fori «length (1/2) downto 1 do
1-208 (C9IT-Sem-5) Introduction
3 MAK HeapllyA,0)
‘Wecan build a heap fom a wnorderd ary in ner tine,
‘The HEAPSORT procedure takes time O(n log) since the call to
[BUILD _HEAP aks tie fn) and eath of the = Las to MAX Heap
takes tine Og.
‘TeAaR] Sort the following array using heap sort techniques +
15, 13,2,26, 7,17, 20,84). Discuss its wort cate and average case
‘ime complexities. (ARTO BOIE-Te, Manis 0]
‘anewer
Given aay: (5, 13,225, 7,1, 20,8, 6
First we eal Bald a be
heapriee Al = 9
P 124403, ell MAX HEAPIFY (A,
{eine call MAX HEAPIFY (i)
‘All BA = 25,Alel=4
Ausain
ausavi
‘Now ell MAX HEAPIFY (4, 3)
‘a3, at
Amat
larpes= 6
‘Aid > A larset)
20317
largest = 7
7, =20
rgest
Ah eA argest
AU> AU
AMsatl
‘Now ell MAX HAPIFY 1.2)
All AU
=, dangest=
Adare] > Ab)
1 laget 20 6-8 Margest)
Now, ausain
AaDAb)
Wo call MAX HEAPIFY 2)
AWeaw
sets
Atanget > A] and args 2
‘AUle> A Mares)
larpot=2, 90/2Design and Analysis af Algorithms
1-27B (CSITSem.5)
asm osttsem Inadton
1B a _ |g
Om
Gg
fh 00 © goo CaERRT TR
lil , get sina AX HEAPEEY A, wept
Alle A Dargest)
Now, All A 7] St
largest i, (largest «Afi OGIO RC)
So foal ree after Bild MAX HEAPLFY
exchange A [1] and AIT and size
Q
x 2Te[s[s[7 [in|
9 06 B
Asin call MAX HEAPIFY (A, 0, we gat
17-126
Now f= 9 downto excha
MAK HEAPIFY U, iene
‘exchanging A (116-4 (9)
exchange (1) and AI6 and now
- =
Terps] :
‘Again call MAX HEAPIFY 4,1),
(eset Tapes]
®
ae tiga ana eect
LOso ®
Grrr.
| ee
* Ari ell MAX HEAPIFY (2)“Mm EE
Design and Analysisof Algorithms 1-291(CST Sy,
‘exchange A {1} and [4 and
©
fY
‘all MAX HEAPIFY (4,1)
exchange A (0 and (3, size = 3-162
re
cries at tase
oo
2T+[s[7[*|s["] =]
‘Thus, sorted array:
Average case nd worn ease compeaty
‘Wehave sen that the running tine of BUILD-HEAP bs fn.
The hespsort algorithm makes acl BUILD-HEAP for eeating 2
man hap which wil ake Oy tine and each ofthe r= elo
MAX-HEAPIFY 1x up the new heap (wich fs ecaed fer
exchanging theron ny dsreatng oe es
4 Welnow MAKHEAPIFY takes tine Og)
‘hath total unig te for the heap sr is On og.
Inteedeion
1-201 CHIT Sen)
GueTAE] Wht in heap sort 7 Apply heap sort algorithm for
sorting 12344, 5,0,7,8%9, 10.Also deduce time complexity of heap
rt
Taswer
Heap nor : fer Q.L22, Page 1-26, Unit
Numeral Since the given problem isresy in nad frm, So, teres
eed to aply any procedae on given problem.
Gueiae [explain HEAP SORT on the array. Ilustrate the
“poration HEAP SORT onthe arrayA=(6,14, 325,210,207),
(ARTO 2017-8, Marks 1)
[ewer
Heap sort: Refer Q 1.2, Page 1-248, Unit
Numerical
‘Originally the given aray (6, 143,25, 2, 10,2, 7,6
Firt ne all Bud Max heap
Despite Al =9
f= 4 call MAX HEAPIFY GA, )
est we cll MAX HEAPIFY(, §)
‘AU)=7,Alil=A 4)935,4 0
Teter l=8
reright l= 9
8 25 (Paleo)
‘en largest
959 and6>25 (Fue)
‘Thea, lergeet = 4
Auleatd
‘Now ell MAX HEAPIFY (2)lotrotstion
Now { = 9 down to 2, and sae = nize ~ 1nd call MAX HEAPIPY
Ua Dose tine
exchanging A] ¢ A)
7 Le a =
ow eal MAX HEAPIFY (2 we pet
Now exchange Aad (and see #8127
[ele [2 Te [33]
‘gala call MAX HEAPIFY(A, 0, ot
exchange (1 and A (and see» 7—1=6
fia) 9.
OY DO). T wD
© ©© © © ©© ®
a sT7Te[e[2]e iu)
Sofina tre afte ‘Agnin ell MAX HEAPIFY (A, 0.9
ater BUILD: MAX HEAP is . ass
®
aq a | = te
K OG Ae
6 ® ca EEE]
‘wit