Answered step by step
Verified Expert Solution
Question
1 Approved Answer
2. A list of n distinct integers a1, a2,..., an is called a mountain list if the elements reading from left to right first
2. A list of n distinct integers a1, a2,..., an is called a mountain list if the elements reading from left to right first increase and then decrease. The location of the peak is the value i where a; is greatest. For example, the list 1, 2, 3, 7, 6 is a mountain list with a peak at 4 since the greatest number occurs in the fourth position. We also consider a list of n increasing numbers to be a mountain list with a peak at n, and a list of n decreasing numbers to be a mountain list with a peak at 1. (a) Give pseudocode for an algorithm based on linear search that takes as input a mountain list a1, a2,,an and returns the location of the peak. procedure LinearPeak(a1, a2,..., an: mountain list) 1. for 1 ton-1 2. if 3. return i 4. return n (b) (5 points) Give an algorithm based on binary search that takes as input a mountain list a1, a2,..., an and returns the location of the peak. procedure BinaryPeak (a1, a2, ..., an: mountain list) 1. i = 1 2. 3. 4. j = n while (i < j) m == [i+1] 5. if 6. i = m +1 7. else 8. j := m 9. return i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Heres the corrected pseudocode for part a and a solution for part b a Pseudocode f...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