3a-twoup - Analyzing Calls (Nonrecursive) SimpleSelect ( A...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Analyzing Calls (Nonrecursive) SimpleSelect ( A [1 ..n ] , k ) Sort ( A [1 ..n ]) return A [ k ] Using InsertionSort : (1) + ( n 2 ) ( n 2 ) . 1 Analyzing Recursive Calls MaxSuffixTD ( A [0 ..n 1] ) if n = 0 return else return Max (0 , A [ n 1] + MaxSuffixTD ( A [0 ..n 2])) Let f ( n ) give the worst-case running time on an array of size n . Then f ( n ) (1) + f ( n 1) for n > . 2 Theorem 3.31 Let f ( n ) af ( n 1) + X ( b n g ( n )) for n > n , where n N , a 1 and b 1 are real numbers, g ( n ) is a smooth function, and X is either O , , or . Then f ( n ) X ( b n g ( n )) if a < b X ( na n g ( n )) if a = b X ( a n ) if a > b Therefore, MaxSuffixTD runs in ( n ) time. 3 MaxSumTD ( A [0 ..n 1] ) if n = 0 return else return Max ( MaxSumTD ( A [0 ..n 2]) , MaxSuffixTD ( A [0 ..n 1])) f ( n ) f ( n 1) + ( n ) ( n 2 ) 4 Maximum Subsequence Sum Revisited We can reduce an instance of size n > 1 to instances of size n/ 2 and n/ 2 . The maximum of the solutions to the smaller instances does not include any segments that start in the first instance and end in the last instance. We therefore need to find the maximum suffix sum of the first instance and the maximum prefix sum of the second. 5 MaxSumDC ( A [ lo .. hi ] ) if lo = hi return Max (0 , A [ lo ]) else mid ( lo + hi ) / 2 ; mid1 mid + 1; sum1 MaxSumDC ( A [ lo .. mid ]) sum2 MaxSumDC ( A [ mid1 .. hi ]) sum3 MaxSuffixBU ( A [ lo .. mid ]) + MaxPrefixBU ( A [ mid1 .. hi ]) return Max ( sum1 , sum2 , sum3 ) 6 MaxSuffixBU ( A [ lo .. hi ] ) m // Invariant: m is the maximum suffix sum of...
View Full Document

Page1 / 11

3a-twoup - Analyzing Calls (Nonrecursive) SimpleSelect ( A...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online