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

Divide & Conquer 04 _ Class Notes

The document is a lecture on Divide and Conquer algorithms, focusing on QuickSort and its implementation. It covers the partitioning process, time and space complexity, and includes multiple-choice questions to test understanding. The lecture provides a detailed explanation of the QuickSort algorithm and its performance in different scenarios.

Uploaded by

bheeshmachava19
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)
0 views

Divide & Conquer 04 _ Class Notes

The document is a lecture on Divide and Conquer algorithms, focusing on QuickSort and its implementation. It covers the partitioning process, time and space complexity, and includes multiple-choice questions to test understanding. The lecture provides a detailed explanation of the QuickSort algorithm and its performance in different scenarios.

Uploaded by

bheeshmachava19
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/ 38

Data Science

& AI
Algorithms

Divide and Conquer

Lecture No.- 4 By- Aditya sir


Recap of Previous Lecture

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

1 Divide and Conquer based sing


to perform
2 Uses partition process algo
divide

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

19 a[m] := a[j]; a[J] := v; return j;


Os returns the correct position
20 }
f of the pivot
element
Topic : Divide and Conquer
1 Algorithm Interchanged(a,I,j)
Is sealing alillaf
2 i with a[j].
// Exchange a[t]
mm
3 {
4 p := a[i];
5 a[i] := a[j]; a[j] := p;
6 }

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
ijmm­pi.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

2. // Sorts the elements a[p],..., a[q] which reside in the global

3. // array a[1 : n] into ascending order: a[n + 1] is considered to


4. // be defined and must be > all the elements in d[l : n].

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

Best Case Sost when isp is


of Quick
used
6
After partition In

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

271N 0in a ofn


2

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

the size of the problem


thereby reducing
by
at every step

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

when Input array is alreadysosted


This happens
order
17 in ascending
5 Tn
12 Ilp A
025 20 11 15 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

For Base Condition

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

input is already sorted

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

C C1 = C2 D We cantsay anything for arbitrary n

Amico
Son
Ipl i n as I order

Fpr n I due 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

You might also like