Peak finding algorithm

◇◆丶佛笑我妖孽 提交于 2019-11-28 09:46:51

Yes, this algorithm only finds a single peak.

If you want to find all the peaks you have to check all the elements, so it's going to be O(n).

Note: a peak is not necessarily a global maximum.

I am not quite convinced if this algorithm is the best way to find an interesting peak. It tends to favor the comparison at middle element which might drive the search to suboptimal direction and eventually the algorithm would always end up in finding peak at Edges and not in the middle. Simple example

[1,2,3,4,5,6,7,8] => Peak would be 8

[6,21,7,8,9,10,11,13] => Peak would be 13 while peak of 21 is more interesting

sure, the algorithm is guaranteed to find a peak because it moves in higher direction but as I show in the example, the peak may not be the interesting one!

I just started this course yesterday, and the problem statement is:

Problem: Find a peak if it exists (Does it always exist?)

So, what the algorithm does is just trying to find a peak, the first one available, in the least time possible.

That's why this algorithm finds only a single peak.

This algorithm looks quite similar to binary search algorithm. Binary search works only on sorted arrays, and this peak-searching algorithm looks like it is supposed to work with another definition: x[p] is a peak if for 0 <= i < p x[i] <= x[i + 1] and for p <= i < x.size x[i] >= x[i + 1]. If we decide that the definition in the question is wrong, and this one is right: everything is OK. And it looks like it is wrong, because it gives several peaks in the second case, you are right.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!