Lectures 6 and 7: Merge-sort and Maximum Subarray Problem
COMS10007 - Algorithms
- Dr. Christian Konrad
18.01.2019
- Dr. Christian Konrad
Lectures 6 and 7 1 / 22
Lectures 6 and 7: Merge-sort and Maximum Subarray Problem COMS10007 - - PowerPoint PPT Presentation
Lectures 6 and 7: Merge-sort and Maximum Subarray Problem COMS10007 - Algorithms Dr. Christian Konrad 18.01.2019 Dr. Christian Konrad Lectures 6 and 7 1 / 22 Definition of the Sorting Problem Sorting Problem Input: An array A of n numbers
Lectures 6 and 7 1 / 22
Lectures 6 and 7 2 / 22
200 400 600 800 1000 1200 1400 200000 400000 600000 800000 1e+06 1.2e+06 1.4e+06 1.6e+06 1.8e+06 secs
Lectures 6 and 7 3 / 22
Lectures 6 and 7 4 / 22
Lectures 6 and 7 5 / 22
Lectures 6 and 7 6 / 22
Lectures 6 and 7 7 / 22
Lectures 6 and 7 7 / 22
Lectures 6 and 7 7 / 22
Lectures 6 and 7 7 / 22
Lectures 6 and 7 7 / 22
Lectures 6 and 7 7 / 22
Lectures 6 and 7 7 / 22
Lectures 6 and 7 7 / 22
Lectures 6 and 7 7 / 22
Lectures 6 and 7 7 / 22
1 Copy left half of A to B: O(n) operations 2 Copy right half of A to C: O(n) operations 3 Merge B and C and store in A: O(n) operations
Lectures 6 and 7 8 / 22
2⌋] ← MergeSort(A[0, ⌊ n 2⌋])
2⌋+1, n−1] ← MergeSort(A[⌊ n 2⌋+1, n−1])
Lectures 6 and 7 9 / 22
Lectures 6 and 7 10 / 22
Lectures 6 and 7 10 / 22
Lectures 6 and 7 11 / 22
Lectures 6 and 7 12 / 22
Lectures 6 and 7 13 / 22
Lectures 6 and 7 14 / 22
0.5 1 1.5 2 1e+06 2e+06 3e+06 4e+06 5e+06 6e+06 7e+06 8e+06 9e+06 1e+07 secs
Lectures 6 and 7 15 / 22
1 A performs two recursive calls on input sizes at most n/2 2 The conquer operation in A takes O(n) time
Lectures 6 and 7 16 / 22
Lectures 6 and 7 17 / 22
50 60 70 80 90 100 110 120 2 4 6 8 10 12 14 16
Lectures 6 and 7 18 / 22
50 60 70 80 90 100 110 120 2 4 6 8 10 12 14 16 50 60 70 80 90 100 110 120 2 4 6 8 10 12 14 16
Lectures 6 and 7 18 / 22
Lectures 6 and 7 19 / 22
Lectures 6 and 7 19 / 22
1 Maximum subarray is entirely included in L 2 Maximum subarray is entirely included in R 3 Maximum subarray crosses midpoint, i.e., i is included in L
Lectures 6 and 7 20 / 22
2
2 +1 A[l].
2
2 +1 A[l] is maximized
Lectures 6 and 7 21 / 22
Lectures 6 and 7 22 / 22