I saw the peak1d
algorithm in here and on Peak finding algorithm.
I can't understand why it surely finds a peak if it exists. It seems that we are deciding to go with one half and can miss a peak on the other. I don't understand how comes you can apply a "binary search" technique for a random array (The array has no prior attribute).
How can I prove that if there is at least one peak in
athe following algorithm will find one of the peak
.
import random
a = [random.randint(0,100) for i in xrange(10)]
def peak1d(a,i,j):
m = (i+j)/2
if a[m-1] <= a[m] >= a[m+1]:
return m
elif a[m-1] > a[m]:
return peak1d(a,i,m)
elif a[m]<a[m+1]:
return peak1d(a,m+1,j)
print a[peak1d(a,0,len(a))]