Divide & Conquer 04 _ Class Notes
Divide & Conquer 04 _ Class Notes
& AI
Algorithms
Topic Mergefort
Code
Topic
Time Complexity 0 nogs
Topic space Complexity O n
not in place
Pyeutions
Topics to be Covered
Topic
QuickfE
Topic
Example
it
Topic ST
TE
[MCQ]
#Q.If one uses straight two-way merge sort algorithm to sort the following
elements in ascending order:
20, 47, 15, 8, 9, 4, 40, 30, 12, 17
then the order of these elements after second pass of the algorithm is:
A 8, 9, 15, 20, 47, 4, 12, 17, 30, 40 B 8, 15, 20, 47, 4, 9, 30, 40, 12, 17
C 15, 20, 47, 4, 8, 9, 12, 30, 40, 17 D 4, 8, 9, 15, 20, 47, 12, 17, 30, 40
x̅ I
Soh A 20 47 15 8 9 4 40 30 12 11
Flog 1 Ypasses
11211511712 17 sorted
me A 3ʳᵈ panop
Auto 20 9047
1719115 30
1517
I
pecergusth after
Its Mr Ito Pass
I
15811514119 130110 1121117 Up after
F
4
To it 15 8 9 4 40 30 12 17 pass
Quick Sost
6
Partitionproless Didi
85 60 55 50 45
75 80
A 65 70
given
we take 1ˢᵗ element as pivot
Note usually
feretition will be placed at its
correctpositioninthe
1 Pivot
Edist at its correct position
gets placed will be
2 After the pivot than pivot
that less
are
all the elements
after partitioning will
on its left
elements greater
pivot
than
all the
3 Similarly
on its right afpartitioning
6 be placed
Topic : Divide and Conquer
1. Algorithm Partition(a,m,p) thmpm q.gl Pint
2. // Within a[m],a[m+1] ,….., a[p–1] the elements are
3. // rearranged in such a manner that if initially t = a[m],
7ᵗʰ
4. // then after completion a[q] = t for some q between m
D Leed
5. // and p – 1, a[k] < t for m < k < q, and a[k] > t
6.
IN
// for q < k < p. q Is returned. Set a[p] = ∞.
gisthe position
7. { imn of pivot after
8. v : = a[m]; i := m; j := p; partition
9. repeat OOI
10. {
Topic : Divide and Conquer
11. repeat
12 i:=i + l; i it
13 until (a[i] > u); while A fi pivot
mum
14 repeat
15 J : = J + l; do
j j 1
16. until (a [i] > v); while
ACjIIrot
17 if (i < j) then Interchanged (a,i , j);
swap a i alj
18 } until (i > j); while icj N
e
is 50 556 6s to 15 8 85
sited
J
ExamePartition h.AT romans
1
yin 2 3 4 5
0
65 7 8 9710
9999
Given
0 58 0 85 605 80 5 5 04 5
461157 75 70
45
mm
to 55
inx
ijmmpi.IE
smaller than 65
65
IT
greater than
65
1 2 9
swap 175s
3 8 Swap 75750
4 7 swap 80755
5 6 scrap 85 60
6 5 no swap on i j
6 return 5 lexitloop
Explanation lode description
of
m p 1 P
Before A O
partition
correct position
Pirt To
after
p of pivot
q
0 partition
After
partition p
pivot pivot
M Kca
arc KCP
91k pivot
6
alk pivot
P Dq T
Topic : Divide and Conquer
A 30
1.
Stats
Algorithm QuickSort(p, q)
send
5. {
E
6. if (p < q) then // If there are more than one element Problem is large
7. {
8. // divide P into two subproblems. Divide
9. j := Partition(a, p, q + 1); on
10. meth
// j is the position of the partitioning element.
Topic : Divide and Conquer
11. // Solve the subproblems.
12. QuickSort(p, j — 1); SPI
13. QuickSort(j + 1,q); SP2
14. //There is no need for combining solutions.
15. }
16. } p a
Qs p 9
p j or
SOC
QS p j 1 as jt 9
Idea of
quiet
as 951
I
Foto
180 I sorted
6
sort
merge
mafic
Cms merge combine
95
d
OD
0511 DSC
pivot
6 III ofbI
Complexity analysis Quick lost
Performance of
Tcn
Problem P
gets placed
the pivot always
Cases After partitioning the
5
at the middle position thereby dividing
array into two realalmostequlparts
MITTLET
1 and
This
n s
OD DOD
Mu
1 n
D
T
partition OI
6
Case Time Complexity Recurrence
T n T n TT n 2 0 n a
II 2TIIT ninig
ftins.no nlog2nf
12
II cases
when after partifition
the pivot always gets
or at the last position
placed either at the 1ˢᵗ position
p ilp
Sort when
Worst case
of Quick array
sorted
is already
asc or desc
17
n
a 1
17 20 25 Tin
15
A Ilp 10
Ten 1
10
5
n I
Tfn 2
10
17 25 Tfn
200 1
no
gtfoIn
Tin 2
n 2
sorted
when input array
is already
This happens
in descending order
17
Recurance of Cases
Time Complexity
legisey
Ten
off
n 1
Tln Tln 1 O n
T n 1 n
T n
partitioning
Subproblem
Tfn 0 n2
V.IM
17
T n T n
1 1
Egin
Tfn T n
I n n
1
Tin 3 n 2
1 n
T n T n
3
n z
17
In general
Tcn T n k n n 1 n k 1
n k 1 K in 1
d.fm
TCIntln
Tin kti
Tas
17 Er may 1
T n
111 1
n I
Hm OInI
17
Time Complexity Summary V.INT
Bestcase MED
unsorted
input is
TC
nlog1
0
Worst case F Of DO
ifp diss order
order
ilp ase
17 1cm
Space Complexity Analysis Quick Sort
of
Bastase
If
1
1
my 2k n Best Case
17
10925
112 logs
2 Worst case
tiara
011
worst
case
mPc
If
SC
3
17
Angle
[MCQ]
e
#Q.Let P be a quick sort program to sort numbers in ascending order. Let t1 and t2
I
be the time taken by the program for the inputs [1 2 3 4] and [5 4 3 2 1]
respectively. Which of the following holds? e Is
ti ipl asc
tz ip2 disc
A t1 = t 2 B t1 > t
C t1 < t 2 D t1 = t2 = = 5 log 5
III Asc order i p worst case
of Quick Sost
duc order i p
ORI
Asymptotically ONI
Absolute terms t 1 4
HEAD
to nz
É
17
[MCQ]
#Q.Let P be a Quick Sort Program to sort numbers in ascending order suing the
first element as pivot. Let t1 and t2 be the number of comparisons made by P for
the inputs {1, 2, 3, 4, 5} and {4, 1, 5, 3, 2} respectively. Which one of the following
holds.
A t1 = 5 B t1 < t 2
C t1 > t 2 D t1 = t 2
Sofa Input 1,2 3 4,5 MES
sorted ase
001 we
Inputz 4 2 mar 5
1,5 3,2
unsorted
t
17
[MCQ]
#Q.Quick Sort is run on two inputs shown below to sort in ascending order
taking first element as pivot
i. 1, 2,3 . . . . N
ii. n, n – 1, n – 2, . . . , 2, 1
Let C1 and C2 be the number of comparisons made for the inputs (i) and (ii)
respectively. Then,
A C1 < C2 B C1 < C2
Amico
Son
Ipl i n as I order
Quick Sol
IPI IP Worst care of
T
11
1
Comp G
17
[NAT]
He
#Q.An array of 25 distinct elements is to be sorted using quicksort. Assume that
the pivot element is chosen uniformly at random. The probability that the pivot
element gets placed in the worst possible location in the first round of partitioning
(rounded off to 2 decimal places) is ___.
2 mins Summary
Topic
Topic
Topic
Topic
Partition Algo
THANK
Sort
- YOU
Quick
Time space complexity
ExamplesTelegram Link:
https://t.me/AdityaSir_PW
20