7/25/16
Divide and
Conquer
Break a problem into two or
more sub-problems and
combine those solutions to
produce the larger answer.
1
2
Advantages of Divide and
Conquer Algorithms
3
Disadvantages of Divide and
Conquer Algorithms
4
Merge Sort
5
6
1
7/25/16
Merge Sort
Input: an array, A of n sortable elements
Output: a permutation of A in non-decreasing order
func MergeSort(A)
if n = 1 return A
mid <- A.length / 2
B <- MergeSort(A[0...mid-1])
C <- MergeSort(A[mid...n-1])
return Merge(B, C)
7
8
9
10
Merge Arrays
Input: two sorted arrays B and C
Output: a sorted array A that combines B and C
func Merge(B, C)
i, j <- 0
A <- []
while i < B.length and j < C.length
if B[i] <= C[j]
A.add(B[i])
i <- i + 1
else
A.add(C[j])
j <- j + 1
if i = B.length
A.addAll(C[j...])
else
A.addAll(B[i...])
return A
Master
Theorem
T (n) = aT
11
⇣n⌘
b
+ f (n)
12
2
7/25/16
T (n) = aT
⇣n⌘
b
Merge Sort⇣ ⌘
n
T (n) = 2T
+n
2
T (1) = 0
+ f (n)
If f (n) 2 ⇥(nd ) where d
8
d
>
<⇥(n )
T (n) 2 ⇥(nd log n)
>
:
⇥(nlogb a )
0
a < bd
a = bd
a > bd
13
14
Input: a set of two dimensional
points, P ={p1, p2,…, pn}
Closest Pair
Output: the pair of points (pi, pj)
that minimizes the Euclidean
distance between them
15
16
Closest Pair
Divide the points evenly into two lists by some line
Recursively call closest pair on each half
Find the smaller distance from the two halves
Eliminate all points farther than the min distance from the dividing line
Scan the remaining points for a distance less than the min distance
17
18
3
7/25/16
Time Complexity
• Presorting
• Dividing
Convex Hull
• Conquer
• Combining solutions
19
20
21
22
Convex Hull
Time Complexity
• Presorting
Divide the points evenly into two lists by some line
• Dividing
Recursively call Convex Hull on each half
Connect the top-most points from each hull and check adjacent points
• Conquer
Connect the bottom-most points from each hull and check adjacent points
• Combining solutions
Remove any points inside the combined hull
23
24
4
© Copyright 2026 Paperzz