Algorithms COMP.5030
Prof. Hugo A. Akitaya
Incremental paradigm recap
• Insertion sort:
• Start with trivial case (A[1] is already sorted by itself)
• Assume 𝐴𝐴[1. . 𝑗𝑗 − 1] is already sorted. Now “insert” 𝐴𝐴[𝑗𝑗] to this sorted order
to make 𝐴𝐴[1. . 𝑗𝑗] sorted.
• In the end, the whole array 𝐴𝐴[1. . 𝑛𝑛] will be sorted.
• Runtime: 𝑂𝑂(𝑛𝑛2 )
Divide and Conquer
• Divide the problem into smaller instances of the same problem.
• Conquer the subproblems with recursion.
• Combine the solutions of subproblems into a solution for the original
problem.
• Incremental paradigm can be seen as divide and conquer with only 1
subproblem.
• Exercise: Write Insertion Sort recursively.
Example
• Divide
• Conquer
• Combine: Merge
Example
• Divide
• Conquer
• Combine: Merge
Pseudocode
Analysis
• T(n): time for sorting 𝑛𝑛 elements
• Divide: Θ(1)
• Conquer: Θ 1 + 2 ⋅ 𝑇𝑇
• Merge: Θ(𝑛𝑛)
• 𝑇𝑇 𝑛𝑛 = �
2 ⋅ 𝑇𝑇
Θ 1
𝑛𝑛
2
𝑛𝑛
2
+ Θ(𝑛𝑛)
if 𝑛𝑛 = 1
otherwise
Recursion tree method
• 𝑇𝑇 𝑛𝑛 = �
2 ⋅ 𝑇𝑇
Θ 1
𝑛𝑛
2
+ Θ(𝑛𝑛)
if 𝑛𝑛 = 1
otherwise
Recursion tree method
• 𝑇𝑇 𝑛𝑛 ≤ �
2 ⋅ 𝑇𝑇
𝑐𝑐
𝑛𝑛
2
+ 𝑐𝑐𝑐𝑐
if 𝑛𝑛 = 1
otherwise
• Relies on noticing a pattern.
• For a formal proof one must prove that the pattern holds for all levels.
• We will not be very formal with this method in this course.
4-Merge-Sort
• 𝑇𝑇 𝑛𝑛 ≤ �
4 ⋅ 𝑇𝑇
𝑐𝑐
𝑛𝑛
4
+ 𝑐𝑐𝑐𝑐
if 𝑛𝑛 = 1
otherwise
Another recurrence relation
• 𝑇𝑇 𝑛𝑛 ≤ �
4 ⋅ 𝑇𝑇
𝑐𝑐
𝑛𝑛
2
+ 𝑐𝑐𝑐𝑐
if 𝑛𝑛 = 1
otherwise
Another recurrence relation
• 𝑇𝑇 𝑛𝑛 ≤ �
𝑇𝑇
𝑛𝑛
4
𝑐𝑐
+ 𝑇𝑇
𝑛𝑛
2
+ 𝑐𝑐𝑛𝑛2
if 𝑛𝑛 = 1
otherwise