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

algo unit 3

Uploaded by

newixe8610
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)
8 views

algo unit 3

Uploaded by

newixe8610
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/ 18

UNIT III

Algorithm Design Technique


3 Divide and Conquer
Methodology
Syllabus
Divide and Conquer methodology : Finding maximum and minimum - Merge sort - Quick sort.

Contents
"Marks 16
3.1 Divide and Conquer Methodology Dec.-09, May-18,
3.2 Finding Maximum and Minimum
3.3 Merge Sort May-08, 14,15,16, 18,
Dec.-11, 12,13,17, Marks 16

3.4 Quick Sort Dec.-16, May-17, 19, Marks 16

3.5 Two Marks Questions with Answers

(3 - 1)
we
sometimes page.) method.
this
can For The The
and time
Where "
Algorithm DC(P)
"
3.1 Algorithms
conmbining
required i) In
relation. The
recurrence abstraction Hence A The If i ) i )
example,
solve generated divide
T(n) control th e TheseDivided vide
di Divide
the
recursive T(n)computing
= returnApplywhere Divide
n1 retum ifP
else
recursivegenerated Combining
sub
and to
problems sub into and and
is
thesolve is
problem if sub
we conquer
the combine DC (P) solution too abstraction
flow a
CATIONS
TECHNICAL
algorithms
want problems
solutions
small time
T(n) g(n)
time to and small
algorithms sub problems conquerConquer
al
smaller
each of the 3-2
as +T(n2)+.......(n obtain of
then control problems are
f or (DC(P,),DC(P,..
of P. for solutions
compute are are
to technique inputs.
todivide above subproblem large amethod,
re sub
divide are
problMetems.hodology
sub PP...n
o solv ed
used
usually problems. The and procedure a used arenough e of a
is procedure and sub given Algorithm
sum in as F(n)
conquer
usually
in then independently.
divide of shown (DC(P; conquer
of same is ,)
+ divided problems problem Desigrn
an n Let the F(n) of of divi
is
numbers and
up-thrust
type by ustime of divide given. is same vide MethTechni
odolog que
conquer now size and into is,
Fig. when smallifn is as and
as required AU
for
n. and conquer typetheas
given a
owledge then the 3.1.1. discuss The n conquer solution :1 -
is Dec.-09, Divide
strategy. original conquer
by (See sufficiently
ing(n) below
divide some strategy. 1s
and
dividing of May-18, Conquer
Fig. is original
problem.
reapplied.
is control
using -
the
problem applications the
and 3.1.l given whole.
computing large
problem Marks
conquer on by
16
e ne the
ot !

t e
and
combine,
It
we
Algonithnms
abOve
Or want
T(n)
then to
equation
depends divide (ao
we problem
sub 1 problem
Sub 1
Solutionto size
n/2of
can +
HNICAL upon is sizeTimenfor a a Fig. 3-3
problemn Solution 1 n/2-1)
called set
3.1.1
the up
T(n) Divide
generalsubinstances recurrence
ATIONS constants Number of = of Solution to
Solution
original to Problem AIgornithm
a size a-1) (at
sizen/bTimeforT(n/b) 3.1.2
Fig. (a, and problem
divide n t.. size
of
Design
a, + for into
relation conquer
b dividing f(n) +
n
-
an and subproblem
intorequired
Timefor a an-1) Technique
and size Solution 2
up-thrust technique
order the problemsub 2 problem Sub 1
conquer obtaining of Solutionto size
n/2 of
problem
for of
n/b Drvide -
knowBedge growth taking +
recurrence. time
a,1) and
Conguer
function for
f(n)
size time Methodology
The
f(n). n to
order is
divide

of
Hence Algorithms
Consider 3.1.1
By Taking This Continuing Now Let
weproperty
can recurrence
can logbx a substituting T(6k) a Efficiency
a also >1 T(n)
ak as
write in
= of =
common
=

= be
=
= and
x°8ha =
logarithm, = =
this = = relation
written
a as, [a a b
a a kT(1) kT
KT fashion a a TlaT(bk-2) a²T a aT aT aT(n/b) Analysis
CHNICAL 5b" as
(1)+ (bk- T(6k- [aT (bK-)
(bk-2) (bk-l) (b/b) 2.>
factor be 3-4
= + (6k- Assume +
,logya n a ak ak k) we 3) by + + f(n) of
fb),f(b²) + + f(bk) f(b*)
a
+a²f(bk-2)
+get, 3)using
ATIONS
aj
f(b) f(b)+ ak-1 + aff6k- n Divide
f(6k-2)] (bk-) = Algonthm
2f(b)
a
ak ak-2 f(b') 1) back b*,
and
+ + where
+
+ak-2 af(6k-l)substitution, Design
-
+ (b2) f
+ f(bk) (6*) f Conquer
an af k
up-thrust a
.... + (bk-l) =
1,
Technique
akak .. (b) f 2,
+
for +a + f(bk) + ..
and -
wledge (6k) f f(bk)] .. f(bk) Conquer
Divide

+a°

(bk) f
Methodology

nsesSume
pexity
Ihen
sorting
On For
example - of
Cal.ion:
values Sort(i)
algorithm.
conquer A)i)class. nunbers.
Compare
The Dosiot! a
ofn Exampl3.e1.1erowth
of
Thus rewrite
carnWeAlgornithmS
we
of can
smallest
brute-force function order of T(n) T(n) ak
=
aConsiderthe
presorting the tpresorting
he
find
above Consider and listthe growthT(n)of = the
eficiency f(n).
the
the
ns"T(1)
Smallest
Element
largest algorithmn equation
CHNI
CATIONS
CAL efficiency smallest
10 10 list 40 based 3-5
based the elements
and of of problem
we
elements threealgorithm
get list B)
algorithm and 20
This
depends
of j=
+
1 loBhn
of 20 of algorithms ) Algorithm
elements finding
sorting largest respectively. presorting for f(b/)/al|
solving upon
is 30 30 30
return
first
and the Design
element
the in smallest values
an an based this Technique
up-thrust elements 40 40 50
array problem
fromn algorithm and of
constants
the as Drvide -
for
is Largest
Element 50 50 10
follows last AU: and largest
knowledge O(nlogn), list
elementMay-18, and determine elements and
as a
C) and Conquer
follows -
Marks
The b
then of its in and MethodoBogy
- the divide efficiency an
(7+8) array order
the list and
time as of
are Step1: element
Example:elements sublists.
maxima Question
Review is On) Agonthms
created.
We In
3.2
1. superior based (ii) Time
have
can
are and From
this
Finding Explain algorithm timcomplexity
e The
divided
We Consider
be algorithm, to for Brute
determined.minima divide
will
found both presorting finding
Maximum and
takes Force
=
find the 50 1 out. a the the
O(nlogn). metOnlogn)
90
ECHNICAL
PUBLICA
TIONS
original
6 50 1
list are conquer the Efficiency
elements sorting9of
min This sublist list based hod 3-6
40
40
ofcompared of and technique.
largest the

and process
list 65 7 -5 3 some elements
maximum algorithm. Hence and
takes O(nlog
maxat 5 3
.9 isand
Minimum choosingForthe
element
first Aln)gorithmn
values mid 4 elements the O(n)
smallest
25 8 carried is +
and -9
45 5
from divided Brute time O(1)
4 and
+
Design
respectively
an two 90 6
from out
minimum
them
element and
Force choosingForthe
element
last
O(1)
up-thrust
sublists
75 9 45 5
65 7 at for Technique
which entire
the divide
for Sublist 2 Sublist 1 25 8
real method from .
from mid Conquer
Divide
and
nowledge
Sublist : 75 list.elements
maximum maximum array.the and
each in AU divideand
conquer
1 order Dec.-09, :
sublist. and are
and and presortingThe
subis chosen. to and method Methodoloqy
Marks
minimum minimum obtain conquer
Two hwa 16 takes

Step 3 min Step 2:


maxmin
value value
obtained.
Hence, are
Lombine
Y
Combine
Solutions

further
Now
Ste4:
p
sublists
possible
into
It
is andidAgaividneach
e Algonlhms
we of max
will (1, (1, min
= = and
50 -9 .. 2) division
and
to values,
combine 3) and min,
divide
and 50 max sublist 90 6
50
1
(3) max 50
HNICAL (4.
50 1 of
the
90
1
3-7
(1, Min values the
and 40 2
5) 40 2 values list 40 2 65
.. = list
3) Min -5, (50, 65 create
ATIONS- and 5
3
40 2 from
not
is are
7 -5 3
Algorithm
Max 40,
(4, -9, each obtained. turther
possible. - -5
5) Max = 5) 3
50 sublist. further. 25 8 Design
and sublists. 4
an = 4
Hence
the 50 25 Technique
up-thrust -5 3
Hence 75 45 5
min 45 5 -9 4 Then
we
and Sublists Sublists
for start we 7
45 from Dvide -
5
knowledge max have
combining each and
values divided Conquer
sublist
among the the Methodology
obtain
list
them
Analysis Step 6: StepAlg5:onthns
Hence
Now
single
If Hence, There The min
Thus Combine
min -9= Combine
tree =
final
time are -9 the
T(n) element T(n) 1=T(n) time for (6,
required two and min the
required 1,25,0,40 recursive complete 50 7)
= = = =recursive máx and sublists and
2(2[2T(n 2|2T 2 is T(n/21) max 90
=
=max
1,3,50,-5 40
CATIONS
TECHNICAL (n/ Tpresent
for list (8, 90
for calls 90 (1, 9) 3-8
(n computing
2)+2 computing calls
values is .. -5 3 Min
/8) /4) +T(n/2|)
in
made
3.3.-5,-5
|1.5,509, will formed 5)
+2]+2) +2]+2 the 2
|4,
be are and -9 4
25,
= 65 7

min list in 45,


5, from (6, Max Algorithm
min 3.2.1
Fig. 45
+2 and then + this -9| 1, ..
= when=2n 2 and 9, which 9).
=
8T(n max T(n) when algorithm, 90, 90 Design
-9 Find
an max the
up-thrust /8) will = 90 25 Technique -
0. n 6,7,90,65
out
6 8

+10 >will min


be 2 be for min 65 7
for - 6,90,925
, and
each 75
nowiedge
8 and Divide
max 25
half
8,9,75,25
values
max and
divided 75 Conquer
values
are
Methodology
sublist. obtained. which

are

Continuing Alin
gonithns
then
if({=
=i)minimum
I/Output
None
maximum Algorithm
: IProblem
llcontain Solution:
i) i)
class. p)ip) i1) )
Anput- ExamNegl
ple ecting
AL
u v) iv) i) Suggest What How What fmarval - GORITHM
or A[0];minval
: A[0];
The
4]
Description
are Max
codefoalgorithm
r linear. is
This
elements.
This The maxval.basic
This
of
you
If
ismany doesreturn
naxval-minval; if if
i
-
: 3.2.1 T(n) T(n)
arrayAn
Alil Consider the
algorithm can'tanthe 1l
integers Min this maxval A[i]; -Alil to
Ali]; t minval algorithm =
algorithmalgorithm basicoperation efficiency times
improvement > <n-1 Secret
order 3n 2kl
= this
elements.
and do algor1thm maxval minval
:Val this operation is
A[0, /2-2 fashion
HNICAL Finding
used (i, finds it, do (A|0, the of T(2)
prove the .., 3-9
j, is has class following
magnitude,
as max, ascan is the basic
compute 1-1| . , +k-1 a
min, that or of >,
recursive
index given be the is to
aoperation of
n-11)
TIONS- value min) computed compare maximum
it this 2'
max improvedby time better ? real
n
= Algorithm
to below can't algorithm i) we 2k-1
elements an complexity equation
array the and be computed What
algorithm numbers
:
can
+2k-2 Design
- for done. declare
n array minimum ? is canTechnique
an A. the
up-thrust recurstvely.
The divide
usingan oftimes altogether ?
elements basic the be
O(n). time obtained.
max inside operation?
for The
element Divide -
knowledge and
a
and complexity
efficiency for for If and
mn
finding from indicate we
wll
conquerthe loop. Conquer
put
given the is
class minval O(n). n
Mathodology
efficiency =2
sequence
pseudo for
and then
this
lse
Solution:Let
Example The thenit
Step 22, miandn else

1
13, efficiency
- 3.2.2 if ifMax Max (itj)
mid /2 min I1aN if
haN i)A (A
Divide : 5, (min> (max
determine- For max
newmax
Min Min //end//end
of
if
22 0 22 8, elseol A
i A
the class
)
the 15, <
max val Val A
13 1 list 13 60, following for min \)
TECHNICAL min (nmid (i,
17, hoU this new) new) then
-5
into -min mid,
2 -5 2 31, many algorithm then +
sub-lists list then 1,
max,
ATIONS- -8 47. j,
-8 c0mparisons of new
elements max min) Ahonthnn
15 |/combine
//combine
is
solution
new, I/divide
15 4 linear
trace Desqn
lave min the
an 60 ie. solution
5
up-thrust 60 been the new) list Technique -
O(n).
recursive handling
17 6 made.
for 17 6

nowledge Divide
31 7
31 7 algorithm two
and

47
lists Conquer
47 8 8
for separately
finding eMethodology

Moy

T }
(high return: if if (low
if
yoi<iostream>
namespace
<climits>
Mi n
ng #include
d usi#include
std;
Implementation Step Step min = The Step Step Step Algorithims
max = (max
a[high];min = (min a[low]; 6: 5 min 4: 3:: 2:
- Max(int
== min : -8
Combine Combine
min
low
a<high])
>
a<low|)
<high) and =
max -
5,
We Divide Further
==
al),
= =
max wil
l/only - 17, (5, thleis2)
t divide
1)//two int 8, (0 combine
list
=
CHNICAL low, . . max 6) Combine
22 =
one max 60
= 4) 22 3-
11
(0 the
=and
22 0 22 0
elements and ..
element int 60 60 5 60 5 sublists
7, (0 13
high, (5... 8) 13
ATIONS- .. further 1
17 6 17
in in int 8) the 1) Algorithm
listthe the &min, list and into 5
-5 2
list (0(2). sublists Design
int .. 31 7 31 7
an
up-thrust &max) 2) 8
Techniquo
and 47 8-
15
3
17
8 8
(3, 4 15
for 4) Divide -
knowledge
and
Conquer

Methodology
main()
{ int Algonths
retum 0:
COut "The
cout<<Min a<O];
min a[O];
=
max = i=0;i<n;i++)
} for(intcout<<"\n
cin>>maX,min;
int int
n; a[20],n;
cin>>a<i);cout< Min Min int }
<<Max(a, mid= return; else { if
Max(a,Max(a, { if if if } if(a|low]
"The
<"Enter max = (max a<lhigh]:
a<low]: min = (min max =(max a<low];
a<high]; min = (min
0, How (low
maximum
minimum
element
array mid low, < > << > <
n
-
1, the many +
mid,
+ a<low|) alhigh])
a<high]) a<low) a<high])
ECHNICAL min, element:";
1, high)
elements high, min,
array max); / 3-
12
2;
min,max);
CATIONS element
are max);
there?";
is" is"
Algorithm
<< <<
Design
an max min
up-thrust
<< << Methodot
Technique
on
endl;endl;
for
owledge ConquerDivide
and

lthis Mer3.g3eSort The TheBnterEnter Snter Enter Bnter How Agoorntthms


l ExampleCombineConquer Divide Mergesomet
Nowwiwe rt hod The a the the the the the many
70.
:
: merge min im um maximum
elementelement:element: :elenentelements
: element:
:Consider : ThenPartition
div i
ons i on arrayaray
Merge Divide
20, Merge a sort
splitlist an
is elementele
2050401030
Merge sort input element are
70 Divide
itnwhtoois 30, sl array is
70
20 20
70 the and sub dynamicallysorting te?her 3-
13
HNICAL into array is is
20 30|20
70Divide elements
40,
S2 list
Merge 3070 20
| 5010 5
into and s1 two with algorithm
70 10,
30 30 30 Algorithm
ATIONS 10 20|30 as listssub carriedn AU
20| unique sub elements : Output
sublists. 50, May-08,14,15,16,18,
that Design
3.Fi3g.1. 30 40 list out.
60 sorted sl
40| s2. and uses
1050 consists Technique
5060 |10|40 |4010
- group. s2 the
an 40
up-thrust 10 60 with
70
Merge
10|
Divide of divide
40 three
Dec-11,12,13,17, Divide -
50 n/2
for 50 and
5060 60 elements steps and
knowiedge 60Merge 50
60 Divide
conquer Gonquer
\Divide
Merge :
each. Methodoog7
strategy. Marks

16
In
Solution: Example
3.3.2 Solution : Algonthms
operation Example
18,
29,
Algorithm
of 68, 3.3.1
algorithm
the Writean 32,
Arrange
Merge algorithm 18
29 43,
TECHNICAL for 18
68
29
29
18
t8296 37, the
Merge 38 29
68
18
Divide merge 87, 3-14
on
a2 following
18 0 24,
273843 27
3a27 43Divide sort the to
18
LICATIONS- Merge 24 24 29 12 47,
|310|27
38|4382 43
3827 - list sort 29 32
43
3243| 68
numbers
50.
Refer 38, a
This
3.3.3
Fig. 32 32 34
Algorithmand
is 3.3.2
Fig. the is
sorted
a 433 given
27, 37 43
Merge 43
Divide/ section 43, sorted
37 5 in
an list Merge
8210
9 elements 3,listof list 47 87
37
37
87 3787 24
87 67 increasing Design
up-thrust 9, 50 37 24
3910|82 |39 8210 3.3. 82, 24
24
37
87
Divide
68 37
87
47 8 Technique
for 82
10. 87 47
24
50 9 order
47
knowledge 10
82 10
82Divide 50 -
merge
using
sort. 50 using ConquerDivide
AU 87
A7 MethodoBogy
50
47 AU
May-08,
: 50 merge
:
Dec.-11,
sor,
Marks Shoe bs
Marlks
12
ks
12

llpointerInput:
/Output: Algorithm
IProblem
lelements
Example
286, Time Solution :Ex90,ample Aganthns
45, 3.3.4 Aray
of complexity
Sorted Description: 95,
278, high)thenif(low < arrayMergeSort(int
using
A
102, 3.3.3
Derive
368,
MergeSort(A,low,mid)
Combine(A,low,mid,high) +high)/2
(lowmid array A of
merge 123 Trace
MergeSort(A,mid
+1,high) unsorted
and and the
475, an Refer : 122

sortThis
|
ECHNICAL A[0...n high 12225 |12270
25 also steps
algorithm 12 25 3-
15
389, A[0.n-1],low,
algorithm 25 25 122
elements, as page 122 70
122
compute
0
656, -1] 25
70 25 of
end 25 mergesort
LICATIONS 3 70 1

788, to pointer 21.


- 89 70
70 its
Algorithm
sort low is 89
12 17589 175 time
503, the for high) 3.3.4
Fig.
89
175 3
175 algorithm
126. of as sortingthe 123175
102122 95 complexity
89 4
Design
following 1/sublist
Il
first|/ Il beginning
array 9095 5
-
an mergingsecond
sublist
split
up-thrust A for
Technique
elements the 102 the
10212395 95102123
of list 123 elements
for two 123102 123102 Dvide -
knowledge at
using sublists
AU mid
AU:
122, and
Dec,-12,
nerge
: Conquer
Dec.-13, 25,
sort 70,
MarksMethodology
Marks
techmique 175,
16
89,
16
Combine(A|0...n-1],
Algorithm
low,
{ Solution
wil Example
89,56
Combining
phase Solution: Algonths
Dividing
phase
first
=Alil)then
if(A|i]<
// while(i
divide There : 3.3.5 29645| 28627845 28627845
-lovw; i k
ifj-mid+1//j
45
We
t-low; 27828645
smaller Sort
else } the are 45 286
45
wil
<= list two the 45
278 278 go
TECHNICAL //smaller element |/i // following
mid repeatedly on 3-16
indexforas askas 126 278 278
stages 286368475 36A
k-A[i] 1l
temp[k|
t-k+1ci+1 i copy
and index
index 368 368475 dividing
element is 278 475
LICATIONS present <= j in setof 368 475368
368
475
that for for high)
mid, andmerge the
high)do 368286
right leftarray elements 3.3.5
list Algornthmn
sublist 475
is smaller then Fig. This 475
present in sublist sort. is 389
left temp we the into
399
Technique
Dosign
-
an elenent sublist array ofA wil 1. merge iusing sorted 389
|365689
389 656 389
Iwo
up-thrust in array ofA IKepeatedly 656389 656
right combine. list
475 126
788
656
sublists.
656
to 788
for sublist 788
temp sort 503 389
-
nowledge dividing 788
12, : 788 788 503 Divide
array 656 503 503 503
AU and
126
: 24, 788 656 503 126 126
May-14,
2. 126 Conquer
8, 126
Combining 71, 4 78A
503
126 Methodology
Marks
)2
;
we

Aiyonthms
while(j<=high)do/copy
kk+1 temp[k]Ajl + while(i<=mid)do//copy
12 12 12
jj+1 remaining
8 12
12 kk+1
i-it1
temp[k]A|il temp[k]Ajl
jej+1k lcopy
-k+1
ECHNICAL
6
12 24 24 24 remaining
temp 3-
17
12 24
6 24
8 |
elements that
24 24 8
8 8
elernents smaller
71 71 71
ATIONS- This 12 71 7 Algorithm
of
is
23
23|8 71
4 4 right element
3.Fi3.g6. sorted
a
of
4 23
|4 Design
24
4
4 4 23 sublist left
list 23 to
6 23 6 sublist Technique-
an 56 6 temp
to
up-thrust 23
23
89
|6
6 89
temp
71
6 89 to array
89 56 Diyide
89 89
56
for 89 56
knowledge 56 and
56 56 56
Conquer

Mothodolo9Y
Algorithm Design Technique - Divide and 3- 19
Conquer Algonthms Algonthm Design
Methodol qY Technique-Dryde and
3-18
Algorthms Conguer Methodolog?
see the combine
Logic explanation Let us operation more closely
with the help of some
algorithm consider a list of
elements as Consider that at Some instance we have got two example.
sublist 20. 30. 40. 70 and 10,
To understand above 50, 60,
40 10 50 60 then
30
70
Array A (left sublist)
1
3 4 5 Array A(right sublist)
1
20304070 105060 Applicable part of
Algorithm
mid high
low
Ioitiallvk=0. Then kwill be incrernented if (A[i|<= Aj)
sublists as ternp temp[z] Aul
Then we will first make two 10
/+1
kek+1
0 }

gets elseexecutpart
else
higt
low
5 6 ed of
0

|70203040|
10 50 60 Merge sort (A, low, mid)
Note that A(left sublist)
algorithm
A(nght sublist)
temp[k] Aj)
jj+1
kek+1
This list is further iremaíns
subdivided This list can be there and j is 2030 40|70 |10 50 60
subdivided incremented
1 2 3

|70 20 30 40

2) This list is This list


subdivided can be Merge sort (A, mid+1, high),
subdivided Array A(right sublist)
Array A (left sublist)
Applicable part of
70 20 30 40 10 50 60 2030 4070 10 50 60 Algorithm
This list
) Combine 5) can be
Combine two i
subdivide
As Aljl < A<i] sublists in
copy Alj] to temp array 6) 10 50 10 k= 1. It is advanced later on
temp
Combine two temp
if (A[i]< = Al)
Sublists

1020 temp (k]A1]|


20 70 30 40 Combine two it part of algorithm j-i+1
sublists 1
8 Combine gets executed kk+1
these two 10 50
sublists moves ahead else
| 2030 40 70
Array A (left sublist) Array A (nght sublist) ternp (k] Al
10 50 60 Note that
jremains there j+j+1
and only i is
20 30 40 70 10 50 60 kek+1
(11) Combine two incremented
sublists i
10 2030 40 5060 70

Fig. 3.3.7

TECHNICAL PUBLICATIONS an up-thrust for TECHNICAL PUBLICATIONS - an up-thrust for


knowiedge
knowledge
Let i.e. Let, 2. This i.e. and equationAs
in i.e. Algonithms
us Using Hence
the As Let, 1. 1.
theorem
Master We
apply
case
Using where
recurrence per can
T(2) T(2T|n) =substitution
T(n) T(n) T(n)
substitution the gives a the
T(1) 0=
average
T(n)
a= 2,
= T(n) Master T(1) =0 T(n) recurrence
T(n) master obtain n>
2 hd
= = = = =
us
2l= (3.7.1),
=
b
= = =
1T
2T 2T(n/2) 2T(n/2) Tn/2)relation theorem the (1)
TECHNICAL 2 2T(n/2) T(n/2) theorem
(2k-l) method and (n and (n relation time
on 0=
+
+c-2k equation + + for worst log2n) f(n) logn) 3-22
+ Substitution complexity
c-2k +
cn T(n/2)
cn + method 2.
Merge : = T(n/2)
cn :
ICATIONS case cn for
+ if merge
(3.3.3). cn Sort time i.e. +
a
n = cn of Algorithm
becomplexity b sort Merge
Assume for with
- is
an Design
n d
up-thrust > Sort
=
n 1
of 1. Technique
= usíng
for 2*. merge
nowledge two
sort Divide -
methods
is Conquerand
(n
log
. »n). Methodoloqy
(3.3.4) (3.3.3) . (3.3.2) (3.3.1)
. .

ie. .Equation
becoms,
(3.3.5) But
Hence

the logncnT(n) =log2


But
we per
as Similarly AlItwegonithrns
O(nlog2n)T(n) = assumed (2*) TT(2*) equation T(2k) T(2*) T(2k) T(2) put
average n we
k
= = = = = 24T(2k-4)+
4c-2k = = can= = = = = :k- 1=
k n k-c-2k 2k.0 2kT(1)+2T(2° = 2kT
=
and =
(3.3.4), 2T
write, 22 22 22 22 2 2T
CHNICAL (2T
worst 2*, + (2k-k)+ (2k-3) T(2k-2)T(2k-2) T(2k-2) T(2*-2)(2k-) (2*-l)+ then, 3-23
taking k T(1)k-c-2k )+
case
c-2k = k-c-2k k-c.2k + ++2-c-2k-l4c-2k
2c.2k +c-2k
ATIONS 0, +2-c. +c c2k
logarithm 3c-
time 2k + 2k-l+c- Algonthm
complexity 2k
c. tc2k
on Design
-
an both 2k
up-thrust Tochnique
of sides.
merge
for -Divide
knowledge
sort
and
is Conquer
(n
log
2). Mathodology
(3.3.5). .
void MS::get
data()
void /*
This classMSinclude This
<stdlib.h>
include # # #
<iostream.h>
includeImplementation
TimeAlgonthms
MS::MergeSort(int
if(low
high) mid;
<int function program
public: private: e****** complexity
low,mid);//first
sublist mid cin>>A[0]:
i=0;i<n;it
for(intcout<
+)cin>>n;cout< Display():
void void voidlow,high;
<conio.h>
voigetd_ int intn;A[10):int
data():
is is
= to
(low <"\n <"n Combine(int
MergeSort for
split of
Merge log2n)9(n Best
case
+high)/2;//split
ATIONS.
ECHNICAL low, Enter Enter merge
the
int (int Sort 3-24
list the list sort
high) low,int
elements length into low,int
sublists mid,
the high):
list :\n"; list:":
of int log2n)Average
e(n case Algorithmn
at high);
an mid Design
up-thrustfor
Technique
owledge log2n)O(n Worst
case
-Divide -

and
Conquer

Methodology

void This/ Algonihms


MS::Combinefunction
while(i /=low:
k=low:;
j=mid+1;temp[10]:int i,j,k;int MergeSort(mid+
1,high)://second
sublist
Combine(low,
{
else if(A[i]<=Aj])
ktti i++;
temp[k]A[il; is
//copy while(j<=high) while(i<=mid) } high)(int for
k=low;k<=high;k+ +) <=
mid low,intmerging
CHNICAL the mid,high);//merging
3-
25

j++: &&j<=
temp[k]=Ajk
k+
+;
A[k]=temp[kl:elements temp[k]=A[il:
k++i j++; k+
+:
i++;
temp[k]=Aljl: mid,int the
two
ATIONS- high) Algorithm
from sublists
temp ofsublist and We
elementleftright
of Design
left and We
element sublist sublist compare
right two
is
an array sublist compare sublist.
lesser sublists Technique
sublist toReached
tempelements
Then sublist toReached
tempelements
Then right
then elements
is
up-thrust to oflesser
sublist. sublist than
copy copy right then copy If
A elements element
of at
the right the the leftofthe
at
sublist copy than If tothat the
from Drvide -
for element temp element
knowledge remainingsublist end remainingsublist end that the smaller inleft and
of to
element
from array the
of smaller
temp sublist Conquer
left are right inleft inleft
elements arsublist
e
elements
remaining.sublist array
the
sublist
the
remaining. in
right
the MethodologY
and
of of and
right left
The 60 50 10 40 30elements
20 Enter
70list Enter main()
void MS. Algorithns
Display()
void /
Questions
Review 10
function
3. 2. 1. Z0
Sorted
Explain State Derive the
30
length Merge getch(): =obj.n-1:
obj.high data():clrscr(): obj.MS
obj.get to
and
the
ArTay
40 Sort obj.Display():
obj.MergeSort(obj.low,obj.high): cout<<"\n\t\t
obj.low=0; display
the 50
explain worst i=0;i<n,i+
for(int +)
working
60
Is
..
of
list cout<<"nln
CHNICAL the case 70 cout< <" sorted
of merge :
analysis 7 Merge 3-26
merge array
sort The
ATIONS sort of Sort "<<A[i: "/
algorith1m
algorithm nerge Sorted
\n";
sort Algonthm
and
Output Array
with using
give Is Design
- an suitable
an the ...\n';
up-thrust example.
recurrence Technique-
illustrations.
AU
for
owlodgeDec.-17, : relation
AU: Divide
AU and
and
May- May-16, :
efficiency.May-15, Conquer
18,
Marks Methodoog1
Marks Marks
13 16 8

i) i) i) We
If If If will
Step
Solution:
list
are
34.1element.
Grample
shis the
positions
In
merge smhine
on
"
elements. Conquer array. ohthan thebased array ismiddle is hwoDivide
element. elementsubinelement method Quick QuiB4
ckSortAgonthms
A A
i [pivot]
and[pivot]
1:
Let 5,
division
arrayelements
pivot
greater less division sort
sub
use Assume 3, sort onin to
us Writean
1, : : and The element than in Split : isa
can / following tw o the arrayS is
< > store 9, is of Recursively
the Combine should pivot than the
not A A first 8, basedarray
division all that Sub splitting
right or dynamically
left the sorting
HNICAL (j), [il, elements algorithm 2, element. the 3-27
be 4, the be andequal sub thatarray
then then rules eletment elements,
on are arrays
moved Pivot i Low 7. all algorithrn
decrement
5
5 actual sortelements in middie
of array array
increment in quick of th e
left less All isthe each the each into
carried
sortfor
array sorted the
3
CATIONS then of 3 an
an 1 array value but two Elements that Algorthm
1 array that less out.
swap is
in elements sub
j. i. 9 than
as of
quick based are The uses
A e24 7 pivotas 4782 - writeandits the arrays. more pvot that Design
-
an |i] are three the
These
up-thrust and A(m]
than
less in Technique
element. elements a than stepsdivide AU
A A(m+1)..A(n-1]
A(m-1],A(n]
A(O]. group
elernent
Pivot
pivot 3.4.1
Fig.
high
for |) time Dec.16, :
ofand
knowledge are quick Diyde -
AU: with
complexity to should conquer
Mid forn sortMay-17,19, and
May-17,
Marks Conquer
thanThese
greater
A(m] a be Elements
than that
greater
pivot are are
strategy.
list in as
elements Methoogy
example of right follows Marks
15
sorted In 16
are sub this
Step Algonithms
As
2 j<i
:
Now we
wil
sort
TECHNICAL
PUBLICA
TIONS swap
position
proper A Swap the
Occupiedits (Pivol] As Pivot 13
J |24
Pivothas 0 Pivot i Pivot left A
<ij 2 [i] A [pivot)0 Swap Swap 3-28
1andSwap 1 2|3|1458
sublist Pivot
|5 Pivot
APivot
2 A
1 and 1 sublist
Left
1 31 5 [i] 5 APivot-’0i’
() 2 (]
3 ([] A and
and 1 a1nd 3 and
3
3 A
1

4 3 A 2 ] A
right position
proper 2
Occupied itPivot
s has 3 [j] 1 1 [] 19 Algonithn
- 3 3
an 4 4 89
2|4 48 3

up-thrust
A
[Pivot)SwapAsj<i Swap
sublist
position
proper 2 4
4|7 82 Design
4
OccupiedPivot
has 89|7 Pivot j Pivot
5 Pivot sublist
Right 4
87 &79 [i] Asimultaneously.
8 5 5
and 6 89 5
j+j 5
for
A
6
and i 6
Technique
nowledge its 6
] 7 jej
7 A
i] J |7 7
7 7 7
7
-
Divide
and
Conquer

Methodoogy

while(i<=jdo
) highi- lowi-pivotWoccupies
+1A[low| (- I/theI/Output:
its llaray l/subarray
I/nput:IProblem
(A[low...high])
Algorithm
Partition
dividing
arrangement
m-partition(A([low...high)// l/split Jlorder
Quick(A[low...m-1])
if(low<high)then UOutput: Iland
l/given. IIheAlgorithn Ouick
I/nputwhi:IProbl
ch emThis
list
and A
algorithm +1...high|)
Quick(A[mid
above In
the high elements The -is Algorithm Step There Step Agornit ms
is The
subarray using th e array Creates The An functionsquick
and 4: is 3:
returned Description:
high indicates
position.
proper
partitioning And list of loDescsription
w array no
as the inthe into Quick(A;O.. n-1],low,high)
given partition.
algorithm sort Finally on
swappings
From
indicates
a
the first A two sub thA[...n-1]
with This elemernts twO e in : above
CHNICAL
PUBLITIONS
CA rightmost
element sub
partition
callto sub array
nghtmost Array Let combining
of low the computation 3-
29
array algorithm lists. in us for
arrays which leftnost algorithm
as in
m A(0...n-1| se
performed
e above
is
rightmost indexthe index A left as ascending The mid element them
is partitions pivot is unsorted all
done of most pseudo of sorted element performs above list. the AIgorithm
Pivot )
the index
element the
in 3 43
and algorithm using only
array. codeorder. array inelements
the in 5 sublists
the ascending list sublist Design
3
an pivot of the sorting following
up-thrust the for 78 Technique
of Partition The is list
are of
we
which
get
for recursivegiven. two
the is -
knowledge is important sorted to Dyide
as The be and
given quick sorted
performs
partition list Conquer
belowroutine as is
MethodoloY
is
for
Assume
case
Best : and pivot are return Algonithms
Solving
The
the
If Analysis swap(A[l
the
o w], A j])/
Partition
function
less
The/when
while while(Ai]<=pivot)
(Al Ii] hen i-1:voti ) do do
t>=pi
if(i<=j) iei+1

recurrence array are


T
recurrence T T partitioned alj//rightmost
than
(n) (1) (n)
2T- 8T+3n = = = = T(n) =
is swap(A[i
the
pivot ],Aj)/ swaps
at
2T| 0 2T relationalways index
TECHNICAL
LICATIONS relatiion list right are
n
2k Time is at
+ +n +n sub-list
sortleftto partitioned
for obtained ofthe of
crosses
i 3-30
|+
kn 2n
(1),
required
this pivot. left called the
list
algorithm
+
at in Inside to swap j A{i]
required
sortTime
right to mid, an
other arrange
of and Algorithm
sub-list ordered pivot A[low] Alj]
isthen words the
an and and Design
up-thrust it elements
such
brings manner. pivot
al Ail Technique
n
the
for the is
nowledge partitioning
the fo-Time
r
arraysubrequired elements
occupying -
best Divide
that
case that and
all
efficiency its the Conquer
proper are
elements
greater
Methodolo
posiion
.) han that

1+2+ But
or
We
can
elementstheal
The Worst Hencethbese t
But as
assume
Algonithms
o
as
3 knowwe
1
worst case T(n) T(n)
C(n) write
C(n) + C(n) n= n
in(sorted
caseof =
.. = = = = =
+ 2*
= n=
= = it
the forcomplexity
time
case n kn 2*, k:n 2.0 2*
n+ C(n
as n log2n T then
(n²)
e
(n+ 1)
n
items
lias
list. . array)
stquick becomes the + (1)
3-31

CATIONS
CHNICAL (n -1) Thiscan k kn + T(n)
- OCCurs
sort becomes kn
1) +n -
+ items
n,1 2krr2k
(n graphically be Agotnm
-2) log,n 2k
3 is
3.4.2
Fig. when +
+ Ofn kn Design
.. items
n-2 log,n) :T
+ the
2 (1) Technique
an + represented pivot is
a
1 =
up-thrust 0
Divide -
items
n-3
for minimum or
and
knowledge Conquer
required
time
n-1 n
maximum MethodologY
.
Average
" For Algor1thns
Where T() gives
Thisus, Telescoping
n+1 T(n) Rearranging nT(n)-(n(n-1)T(n The
n+1 T(n) n+12c2c T(n-2)
T(n-1)n The The Ti-me any time
H n+1 T(n) 1 T(n-1), n+1 T(n)
nT(n)
-1)
nT(n) recurrence
T(n) = head for pivot case
is nTis.tt -1)T(n complexity
harmonic
a n-1 above above (n= = = partitioning (Random
and position
n+1-1)2c(H T(0) 2c2c n-1 T(n T(n-1), 2c +1)T(n -1(T(O)+T(1)+....T(n
) 2(T(0) 2(T(0) 1
|i0 relation tail
(1,1 equation equation
n n-1
CHNICAL -2) = subarrays of
T(1)
T(2)
T(0) +....+ 2T(n
+T(1)+....T(n +T(1) +To-1-i)
+en)ST) | array)
i; 3-32
number 3
n
+
-1)+ an i worst
or
ONS- 2 32T(1)
T(2) as, +1
and 2cn -1l) +.....+T(n array e (0,
and
n+1 )n
+ averageconlain . ., case
dividing 2cn = n
H, n 1 -c -2) -2) Cn. ofAlgorithm
n+1 -2) - quick
=1+,+, case 1)
1
by 2T(n +
c(n +T(n +T(n and i
an 1
n(n for sort Dosign
thrust -1) -1)2 -1) - -1n
1 + 1) sorting is
t....* + ) cn
+ + Tochniquo
1) 2cn - O
for 2) cn) (n
edge
n
1 is i
items 2)
-
Divide
respectively.
and
Conquer

Methode

Quick:get_data(intint n)
function
This public: private: Quicl#def
arr[SIZE]:
get_data(int):
partition(int,int);
void int
display
void
swap(int,int); int
quicksort(int,int);
void int
assck i<coni
nSIeZE#in10
clou.deh> Program ProgramC+t
#inc#ilundeclu<destd<libo.shtr>eam.h> Implementation to Average
Agonithms
int i; sort (n+1)c' T(n) =n+1 T(n)
T(n) logn
Element ";cout<<\n
cin>>n;
for(i=0;i<n;it+)
return n;
cout<<\nEnter
cin>>arr[i]; is
to
the case
input (int); O(nlog n) c'log n
elements time
Enter 3-33
CHNICAL
PUBLICAT the
complexity
Total elements in
ascending
numbers
is Algorithm
TIONS® 4
(nlog
to order
sort n) Design
- using
an :";
up-thrust Technique-
Quick

for Sort
knowledge Divide
and
*

Conquer

Methodology
arli]=p;p=arrlil: int p; void retun j;
arrli<=arrlil:
int
arljl=pivot;
=arljl:
arrim]
}while(i<j): dc
i=m,)=p;int int
pivot=arr[m]; This
*/
{ int j: void This
Quick::swap(int swap(ij);:if(i<j)
}while(arri|>pivot); { do do if(p<q) Algonithms
}while(arr|i]<pivot): i++; {
quicksort(p,
Quick:partition(int quicksort(j+
j=partition(p.
function j-1);//splitting
q+1),://setting
eleliment
sof
tpivot Quick::quicksort(intfunction
is is
1,q);//splitting
to to
i,int partition sort
ECHNICAL
PUBLICA
TIONS
j) m,int the
p,int 3-34
p) a elements
list of q)
and list
decide in
a
sublist Algonithm
the
an
up-thrust
element
pivot Design

Technique
for
nowledge
-
Divide

and
Conquer

Methodolog

Questions
Review Element
Enter40 Element
Bnter Enter 10
Element Element
Enter 50 Bnter 30 n,in1t ; }
maivonid() Quick:display(int
woid n)
Enter getiscph(la):y(n); cout<<n\t\tclrscr(): Ouick
objobj.dcout<<n\n\t Aqornthms
for 1.
algorithm. Give
10Is:
Sorted
Array
20
Element 20
Total .n=obj
quicks.goert(_0da,nt-a1(n);:); obj;//tor i=0;i<n;i+
cOut<for(i
< nt +)
"<<arr[i]:
the 30 numbers Method
Quick
Sort
algorithm 40 Sorted Quick integer
50
to
AarTy Sort
CHNICAL
ICATIONS sort:
Method
elements 3-
35

quicksort. 5 Is:
\n':
\n.
Algonthm
With
an Output
example Design
an
show Technique-
up-thrust
that
for
knowledge qucksort Drvide
AU: and
is Conguer
Dec.-16, not
a
Marks stable Methodiogy

SOrting
16|
combining
requiredWhere Ans. : Q.4 Algorithm
DC(P) Algornthms
Ans. : Q.3
problems
sub-problems
4) 1) Ans. problem.
Q.2 obtained.
original
given broken Ans. ORQ.1 |3.5
3. 2.
T(n) Write : :
to Write The What Two complexity.
Prove tcase
ime Write
the returnApplywhere21Divide
n else
retum if
FindingBinary
are intoDivide Give What
solve is a P Later
that the
the T(n) recurrence usuallyproblems the general Marks
solutions is the kind is
=
small time (DC(P,),DC(P,)...
combine DC (P) solution too maximumsearch
control
the on and meant any quicksort
CHNICAL
ATIONS to and small solution problems of smaller
these conquer algorithm
to fo|rT(n,) gn)
inputs. each solved strategy
Questions
2) can by algorithm
sub divide relation obtain of
then abstraction
solutions sub 3-36
subproblem P. andMerge divide
+T(n,)+......T(n,)
problems. The using for be is that
P,P,...n minimum. can problems an of sorts
and equation sortdivide thedivided divide and and
F(n) be algorithmic
ar e with
conquer for
original solved conquer by explain
is divide 3) combined and comparison, Algorithm
Design
the for (DC(P,): Quick and into and Answers it
time of conquer with
an divide and conquerproblem
sub-problems, using the
strategy technique
up-thrust size sort together requies an
required
n.F(n) + divide solutions method.
conquer.
and example. Technique
for The method. can in S2
conquer ?
and towhich (n
owledge ing(n) when smallifn is be lgAU: Derive
dividing and obtain to -
For
obtained. conquer n) Divide
is n these time. the
the is algorithm. example. on the MarksAU: May-19,
May-19,
the worst and
sufficiently
computing
problem solvingmethod solutiOn subgiven AU Conquer
AU This May-16, : case
probleme Marks
Dec.-12, : problem May-13 AU
:
and
large fvne al 2 for Dec-17 5+4+a
average
Methodology

13 t the : :

is
involvedsorting
Ans. Q.6 Ans.
:
Q.5
Algorithms
metinhod No,
quick alIsgoriThe thm. Nameleastat
quick merge
this the sort
sorting sort sort
ordering stable
PUBLI
NICALTICOANS is sorting
two
and 3-37
with not
sorting
of quick
similar
pivot a
stable sort
algorithm techniques Algorithm
element).
elements make
sorting
S-
? use Design
an that
algorithm
may of
up-thrust divide Technique
not make
for be
because and use
knowledge preserved. Divide -
conquer of
after divide and
(Since algorithm. Conquer
applying and
Swapping Methodoiogy
conquer
OD0
this

You might also like