CSE 221: Algorithms: Divide and Conquer
CSE 221: Algorithms: Divide and Conquer
Mumit Khan
References
1 T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, Introduction to Algorithms, Second Edition.
The MIT Press, September 2001.
2 Erik Demaine and Charles Leiserson, 6.046J Introduction to Algorithms. MIT OpenCourseWare, Fall 2005.
Available from: ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/
6-046JFall-2005/CourseHome/index.htm
This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
Binary search
The problem
Find an element in a sorted array:
1 Divide: Check the middle element.
2 Conquer: Recursively search 1 subarray.
3 Combine: Trivial.
Binary search
The problem
Find an element in a sorted array:
1 Divide: Check the middle element.
2 Conquer: Recursively search 1 subarray.
3 Combine: Trivial.
Binary search
The problem
Find an element in a sorted array:
1 Divide: Check the middle element.
2 Conquer: Recursively search 1 subarray.
3 Combine: Trivial.
Binary search
The problem
Find an element in a sorted array:
1 Divide: Check the middle element.
2 Conquer: Recursively search 1 subarray.
3 Combine: Trivial.
Binary search
The problem
Find an element in a sorted array:
1 Divide: Check the middle element.
2 Conquer: Recursively search 1 subarray.
3 Combine: Trivial.
Binary search
The problem
Find an element in a sorted array:
1 Divide: Check the middle element.
2 Conquer: Recursively search 1 subarray.
3 Combine: Trivial.
Binary search
The problem
Find an element in a sorted array:
1 Divide: Check the middle element.
2 Conquer: Recursively search 1 subarray.
3 Combine: Trivial.
Analysis
nlogb a = nlog2 1 = n0 = 1 ⇒ CASE 2 (k = 0)
⇒ T (n) = Θ(nlogb a lgk+1 n) = Θ(lg n)
Merge sort
The problem
Sort an Array:
1 Divide: Trivial.
2 Conquer: Recursively sort 2 subarrays.
3 Combine: Merge the sorted subarrays in Θ(n) time.
Merge sort
The problem
Sort an Array:
1 Divide: Trivial.
2 Conquer: Recursively sort 2 subarrays.
3 Combine: Merge the sorted subarrays in Θ(n) time.
Key subroutine
merge – to merge two sorted arrays in linear-time.
T (n) = Θ(n)
T (n) = Θ(n)
T (n) = Θ(n)
merge-sort(A) A[1 . . n]
1 if n = 1
2 then return
3 else recursively sort the two subarrays
4 A1 = merge-sort(A[1 . . dn/2e])
5 A2 = merge-sort(A[dn/2e] + 1 . . n])
6 A = merge(A1 , A2 ) merge the sorted arrays
merge-sort(A) A[1 . . n]
1 if n = 1
2 then return
3 else recursively sort the two subarrays
4 A1 = merge-sort(A[1 . . dn/2e])
5 A2 = merge-sort(A[dn/2e] + 1 . . n])
6 A = merge(A1 , A2 ) merge the sorted arrays
merge-sort(A) A[1 . . n]
1 if n = 1
2 then return
3 else recursively sort the two subarrays
4 A1 = merge-sort(A[1 . . dn/2e])
5 A2 = merge-sort(A[dn/2e] + 1 . . n])
6 A = merge(A1 , A2 ) merge the sorted arrays
Analysis
nlogb a = nlog2 2 = n1 = n ⇒ CASE 2 (k = 0)
⇒ T (n) = Θ(nlogb a lgk+1 n) = Θ(n lg n)
Powering a number
Powering a number
Powering a number
Powering a number
Conclusion