Question
-Suppose you have an array A with n distinct numbers and the sequence of values is unimodal: there is a value p between 1 and
-Suppose you have an array A with n distinct numbers and the sequence of values is unimodal: there is a value p between 1 and n (inclusive) such that the array entries increase up to position p and then decrease until n. We want to find the peak entry p. We could do this with a linear search: check each entry to see if it is larger than its neighbors. If it is, return that one. Otherwise, check the next one. Before you proceed with reading this, please make sure you understand this problem statement and why the linear search solution works. Instead, we can actually solve this by modifying binary search. Initially, we know the point p is between 1 and n inclusive. Look at the mid-point and check if it is larger than its neighbors. If it is, return that index. Otherwise, determine which side p is on and update our bounds accordingly. (a) Write this in pseudo-code. You may do this recursively or using a loop, your choice. (b) Use a loop invariant (if you wrote with a loop) or a recursive meaning (if you didnt) to prove the correctness of this algorithm.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started