L-2005-05-Divide and Conquer
L-2005-05-Divide and Conquer
Learning Objectives
At the end of this lecture, you will be able to:
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.
Program Code
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)
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
(0)
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
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
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
5 3
j ends, swap pivot with i
i j
0 1 2 3 4 5 6
0 2 3 4 10 40 44
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
3. If 𝑎 < 𝑏 𝑘
a) If 𝑝 ≥ 0, then, 𝑇 𝑛 = Θ 𝑛𝑘 𝑙𝑜𝑔𝑝 𝑛
b) If 𝑝 <0, then, 𝑇 𝑛 = O 𝑛𝑘
Operation on Binary Search 24
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