This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 6.006 Intro to Algorithms Recitation 02 February 4, 2011 1D Peak Finding Objective Given an array A with n elements, find the index i of the peak element A [ i ] where A [ i ] A [ i 1] and A [ i ] A [ i + 1] . For elements on the boundaries of the array, the element only needs to be greater than or equal to its lone neighbor to be considered a peak. Or, say A [ 1] = A [ n ] = . Algorithm Given an array A with n elements: Take the middle element of A , A [ n 2 ] , and compare that element to its neighbors If the middle element is greater than or equal to its neighbors, then by definition, that element is a peak element. Return its index n 2 . Else, if the element to the left is greater than the middle element, then recurse and use this algorithm on the left half of the array, not including the middle element. Else, the element to the right must be greater than the middle element. Recurse and use this algorithm on the right half of the array, not including the middle element. Runtime Analysis When we recurse, we reduce size n array into size n 2 array in O (1) time (comparison of middle element to neighbors). Show recursion in the form of Runtime of original problem =Runtime of reduced problem + Time taken to reduce problem. Then use substitution to keep reducing the recursion. 6.006 Intro to Algorithms6....
View
Full
Document
This note was uploaded on 11/11/2011 for the course MATH 180 taught by Professor Byrns during the Spring '11 term at Montgomery College.
 Spring '11
 byrns
 Math

Click to edit the document details