rec02 - 6.006 Intro to Algorithms Recitation 02 February 4,...

Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the 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.

Page1 / 4

rec02 - 6.006 Intro to Algorithms Recitation 02 February 4,...

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

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