0% found this document useful (0 votes)
48 views25 pages

L-2005-05-Divide and Conquer

The document discusses the quick sort algorithm, including its best and worst case time complexities of O(n log n) and O(n^2) respectively. It provides pseudocode and examples of the partition process and recursively sorting subarrays.

Uploaded by

22004788
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)
48 views25 pages

L-2005-05-Divide and Conquer

The document discusses the quick sort algorithm, including its best and worst case time complexities of O(n log n) and O(n^2) respectively. It provides pseudocode and examples of the partition process and recursively sorting subarrays.

Uploaded by

22004788
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/ 25

1

WIA2005: Algorithm Design and Analysis


Attendance Password: qjs8r3
Students: K1
Lecturer: Dr. Hazrina Sofian (HS)
2

Lecture 5 – Divide and Conquer

Learning Objectives
At the end of this lecture, you will be able to:

Know and understand what are:


1. Merge Sort (We have learned in Lecture 2)
2. Quick sort
3. Binary Search
Quick Sort (Best-case scenario) 3

Concept: Choose a pivot point. Best case when pivot splits the array evenly. Elements smaller
than the pivot are on the left, and elements larger than the pivot on the right.

<= pivot pivot > = pivot

Algorithm / Pseudo code

1. Pick any item in the array to be a pivot


2. If swap, i & j will move
3. If no swapping, only j will move
4. When j ends, swap pivot with i

5. Recursively sort the left & right subarrays

* Pivot can also be randomly selected, known as randomised quick sort


Quick Sort (Best-case scenario) 4

Program Code

<= pivot pivot > = pivot

T(n/2)
T(n/2)

PARTITION(A, p, q) ⊳A[ p . . q]
x = A[p] ⊳pivot = A[p]
i=p
for j = p + 1 to q
do if A[ j]  x
then i =i + 1 O(n)
exchange A[i]  A[ j] Best case – pivot splits the array evenly
exchange A[ p] A[i]
Time Complexity = 2T(n/2) + n
return i = O(nlogn)

* Pivot can also be randomly selected, known as randomised quick sort


Quick Sort (Worst-case scenario) 5

Worst case when one side of partition always has no element.

T(0) – nothing to sort at the left side of the pivot.


T(n-1) pivot > = pivot

PARTITION(A, p, q) ⊳A[ p . . q]
x = A[p] ⊳pivot = A[p]
i=p T(0) – nothing to sort at the right side of the pivot.
for j = p + 1 to q <= pivot pivot

do if A[ j]  x
then i =i + 1 O(n)
exchange A[i]  A[ j] Worst case - One side of partition always has
exchange A[ p] A[i] no element.
Time Complexity = T(0) + T(n-1) + n
return i = T(n-1) + n
= O(n2)
Arithmetic series
Quick Sort
Analyzing Worst Case Time complexity using Recursive Tree

T(n) = T(0) + T(n–1) + cn


cn
n
 k  = n 2
T(0) c(n–1)  
T(0) c(n–2) k =1 
T(0) O

(0)

T(0) – nothing to sort at the left side of the pivot.


Quick Sort Algorithm:-
Operation on Quick Sort 1. Pick any item in the array to be a pivot7
Example: Left-most item is chosen to be the pivot2. If swap, i & j will move
Given array = 6, 10, 13, 5, 8, 3, 2, 11 3. If no swapping, only j will move
4. When j ends, swap pivot with i
pivot  pivot pivot  pivot
5. Recursively sort the left & right subarrays
0 1 2 3 4 5 6 7
6 10 13 5 8 3 2 11 i=0, j=1
i j
A[j] < pivot = 10 <6? No. So no swapping
6 10 13 5 8 3 2 11 i=0, j=2
i j
Quick Sort Algorithm:-
Operation on Quick Sort 1. Find the median using randomized select algo. 8

2. If swap, i & j will move


Given array = 6, 10, 13, 5, 8, 3, 2, 11 3. If no swapping, only j will move
4. When j ends, swap pivot with i
pivot  pivot pivot  pivot
5. Recursively sort the left & right subarrays
0 1 2 3 4 5 6 7
6 10 13 5 8 3 2 11 i=0, j=1
6 10 13 5 8 3 2 11 i=0, j=2
i j
A[j] < pivot = 13 <6? No. So no swapping
6 10 13 5 8 3 2 11 i=0, j=3
i j
Quick Sort Algorithm:-
Operation on Quick Sort 1. Pick any item in the array to be a pivot 9

Given array = 6, 10, 13, 5, 8, 3, 2, 11 2. If swap, i & j will move
3. If no swapping, only j will move
4. When j ends, swap pivot with i
pivot  pivot pivot  pivot
5. Recursively sort the left & right subarrays
0 1 2 3 4 5 6 7
6 10 13 5 8 3 2 11 i=0, j=1
6 10 13 5 8 3 2 11 i=0, j=2
6 10 13 5 8 3 2 11 i=0, j=3
i i+1 j
A[j] < pivot = 5 <6? Yes. So swap A[i+1] with A[j]
6 5 13 10 8 3 2 11 i=1, j=4
i j
Quick Sort Algorithm:-
Operation on Quick Sort 1. Pick any item in the array to be a pivot 10

Given array = 6, 10, 13, 5, 8, 3, 2, 11 2. If swap, i & j will move
3. If no swapping, only j will move
4. When j ends, swap pivot with i
pivot  pivot pivot  pivot
5. Recursively sort the left & right subarrays
0 1 2 3 4 5 6 7
6 10 13 5 8 3 2 11 i=0, j=1
6 10 13 5 8 3 2 11 i=0, j=2
6 10 13 5 8 3 2 11 i=0, j=3
6 5 13 10 8 3 2 11 i=1, j=4
i j
A[j] < pivot = 8 <6? No. So no swapping
6 5 13 10 8 3 2 11 i=1, j=5
i j
Quick Sort Algorithm:-
Operation on Quick Sort 1. Pick any item in the array to be a pivot 11

Given array = 6, 10, 13, 5, 8, 3, 2, 11 2. If swap, i & j will move
3. If no swapping, only j will move
4. When j ends, swap pivot with i
pivot  pivot pivot  pivot
5. Recursively sort the left & right subarrays
0 1 2 3 4 5 6 7
6 10 13 5 8 3 2 11 i=0, j=1
6 10 13 5 8 3 2 11 i=0, j=2
6 10 13 5 8 3 2 11 i=0, j=3
6 5 13 10 8 3 2 11 i=1, j=4
6 5 13 10 8 3 2 11 i=1, j=5
i i+1 j
A[j] < pivot = 3 <6? Yes. So swap A[i+1] with A[j]
6 5 3 10 8 13 2 11 i=2, j=6
i j
Quick Sort Algorithm:-
Operation on Quick Sort 1. Pick any item in the array to be a pivot 12

Given array = 6, 10, 13, 5, 8, 3, 2, 11 2. If swap, i & j will move
3. If no swapping, only j will move
4. When j ends, swap pivot with i
pivot  pivot pivot  pivot
5. Recursively sort the left & right subarrays
0 1 2 3 4 5 6 7
6 10 13 5 8 3 2 11 i=0, j=1
6 10 13 5 8 3 2 11 i=0, j=2
6 10 13 5 8 3 2 11 i=0, j=3
6 5 13 10 8 3 2 11 i=1, j=4
6 5 13 10 8 3 2 11 i=1, j=5
6 5 3 10 8 13 2 11 i=2, j=6
i i+1 j

A[j] < pivot = 2 <6? Yes. So swap A[i+1] with A[j]


6 5 3 2 8 13 10 11 i=3, j=7
i j
Quick Sort Algorithm:-
Operation on Quick Sort 1. Pick any item in the array to be a pivot 13

Given array = 6, 10, 13, 5, 8, 3, 2, 11 2. If swap, i & j will move
3. If no swapping, only j will move
4. When j ends, swap pivot with i
pivot  pivot pivot  pivot
5. Recursively sort the left & right subarrays
0 1 2 3 4 5 6 7
6 10 13 5 8 3 2 11 i=0, j=1
6 10 13 5 8 3 2 11 i=0, j=2
6 10 13 5 8 3 2 11 i=0, j=3
6 5 13 10 8 3 2 11 i=1, j=4
6 5 13 10 8 3 2 11 i=1, j=5
6 5 3 10 8 13 2 11 i=2, j=6
6 5 3 2 8 13 10 11 i=3, j=7
i j
A[j] < pivot = 11 <6? No. So no swapping

6 5 3 2 8 13 10 11 i=3, j=8
i j
Quick Sort Algorithm:-
Operation on Quick Sort 1. Pick any item in the array to be a pivot 14

Given array = 6, 10, 13, 5, 8, 3, 2, 11 2. If swap, i & j will move
3. If no swapping, only j will move
4. When j ends, swap pivot with i
pivot  pivot pivot  pivot
5. Recursively sort the left & right subarrays
0 1 2 3 4 5 6 7
6 10 13 5 8 3 2 11 i=0, j=1
6 10 13 5 8 3 2 11 i=0, j=2
6 10 13 5 8 3 2 11 i=0, j=3
6 5 13 10 8 3 2 11 i=1, j=4
6 5 13 10 8 3 2 11 i=1, j=5
6 5 3 10 8 13 2 11 i=2, j=6
6 5 3 2 8 13 10 11 i=3, j=7
j
A[j] < pivot = 11 <6? No. So no swapping

6 5 3 2 8 13 10 11 i=3, j=8
i j
When j ends, swap pivot with i
2 5 3 6 8 13 10 11
Quick Sort Algorithm:-
Operation on Quick Sort 1. Pick any item in the array to be a pivot 15

Given array = 6, 10, 13, 5, 8, 3, 2, 11 2. If swap, i & j will move
3. If no swapping, only j will move
4. When j ends, swap pivot with i
pivot  pivot pivot  pivot
5. Recursively sort the left & right subarrays
0 1 2 3 4 5 6 7
6 10 13 5 8 3 2 11 i=0, j=1
6 10 13 5 8 3 2 11 i=0, j=2
6 10 13 5 8 3 2 11 i=0, j=3
6 5 13 10 8 3 2 11 i=1, j=4
6 5 13 10 8 3 2 11 i=1, j=5
6 5 3 10 8 13 2 11 i=2, j=6
6 5 3 2 8 13 10 11 i=3, j=7
j
A[j] < pivot = 11 <6? No. So no swapping

6 5 3 2 8 13 10 11 i=3, j=8
i j
When j ends, swap pivot with i Recursively sort the left &
2 5 3 6 8 13 10 11 right subarrays using
quick sort by repeating
left subarray right subarray from step 1. Pick a pivot
Quick Sort Algorithm:-
Operation on Quick Sort 1. Pick any item in the array to be a pivot 16

Given array = 6, 10, 13, 5, 8, 3, 2, 11 2. If swap, i & j will move
3. If no swapping, only j will move
4. When j ends, swap pivot with i
 pivot pivot  pivot
5. Recursively sort the left & right subarrays
2 5 3 6 8 13 10 11

pivot left subarray

2 5 3 A[j] < pivot = 5 < 2? No. So no swapping


i j

2 5 3 A[j] < pivot = 3 < 2? No. So no swapping


i j

2 5 3 j j ends so swap pivot with i


i

2 5 3 So now 2 is already at the right place


right subarray
Quick Sort Algorithm:-
Operation on Quick Sort 1. Pick any item in the array to be a pivot 17

Given array = 6, 10, 13, 5, 8, 3, 2, 11 2. If swap, i & j will move
3. If no swapping, only j will move
4. When j ends, swap pivot with i
 pivot pivot  pivot
5. Recursively sort the left & right subarrays
2 5 3 6 8 13 10 11

pivot right subarray

5 3 A[j] < pivot = 3 < 5? Yes. So swap A[i+1] with A[j]


i j

5 3
j ends, swap pivot with i
i j

3 5 So now 5 is already at the right place

3 Reached base case


2 3 5 6 8 13 10 11

left subarray is sorted. right subarray – try yourself 


18

Done with the concept of


Quicksort. Please try to resolve the
right sub-array.
Binary search 19

Binary search finds the index of a value x within a sorted array.


For Example:

Given array = 0, 2, 3, 4, 10, 40, 44

0 1 2 3 4 5 6
0 2 3 4 10 40 44

What is the index when x=10?

When x=10, the position of the x is at index 4.


Binary search 20

Binary search uses Divide and Conquer Approach


Divide and Conquer Approach (Revision)
Search problem solved using divide and conquer approach.

CLASS ACTIVITY:
You have a thick phone book with names in alphabetical order. Given a
name to be searched, what is your first step?

Step 1: Open the book in the middle (or on a random page). If name
found = DONE!

Step 2: If name is not in the page, you can exclude either left or
right part of the book. Search the remaining part.

Step 3, repeat until step k: Step k is when you have found the name

https://web.eecs.umich.edu/~aprakash/eecs282/lectures/11-recursion.pdf
Binary search 22

T(1)

T(n/2)
OR
T(n/2)

https://www.youtube.com/watch?v=T2sFYY-fT5o

T(n) = T(n/2) + 1
= O(log n)
Binary Search
Analyzing Time complexity using Master Method Way 1

T(n) = T(n/2) + 1 T n = aT nൗb + Θ nk log p n , where ,a ≥ 1 , b


> 1, k ≥ 0, p = real number

a=1, b=2, k=0, p=0 1. If 𝑎 > 𝑏 𝑘 then, 𝑇 𝑛 = Θ(𝑛log𝑏 𝑎 )


a bk
1 20 2. If 𝑎 = 𝑏 𝑘
So, a = bk a) If 𝑝 > −1, then, 𝑇 𝑛 = Θ 𝑛log𝑏 𝑎 𝑙𝑜𝑔𝑝+1 𝑛
b) If 𝑝 = −1, then, 𝑇 𝑛 = Θ 𝑛log𝑏 𝑎 𝑙𝑜𝑔 log 𝑛
T(n) = (log n) c) If 𝑝 < −1, then, 𝑇 𝑛 = Θ 𝑛log𝑏 𝑎

3. If 𝑎 < 𝑏 𝑘
a) If 𝑝 ≥ 0, then, 𝑇 𝑛 = Θ 𝑛𝑘 𝑙𝑜𝑔𝑝 𝑛
b) If 𝑝 <0, then, 𝑇 𝑛 = O 𝑛𝑘
Operation on Binary Search 24

Given array = 0, 2, 3, 4, 10, 40, 44, identify index when x = 10


0 1 2 3 4 5 6
0 2 3 4 10 40 44
Step 1: Identify middle element of the array
Middle element of the array = n/2= 7/2= 3

Step 2: Compares the target value to the middle element of the array.
Since x > 4, search the right halve

0 1 2
10 40 44

Step 1: Identify middle element of the array


n/2= 3/2= 1
Step 2: Compares the target value to the middle element of the array.
Since x < 40, search the left halve

the position of the x is at index 4.


25

Any Question ???

WHAT WHY WHERE WHEN WHO HOW

You might also like