I am not able to disprove the working of this solution, so I would be happy if somebody could.
Consider array 'a' indexed from 1 to n.
low = 1
high = n
mid = (low + high)/2
Without loss of too much generality, I am going to assume the array values are distinct.
So,at mid, the a[mid - 1], a[mid], a[mid + 1] can be like:
Case 1:
/\
Case 2:
\/
Case 3:
/
/
Case 4:
\
\
Case 5(boundary):
/
Case 6(boundary):
\
&m = mid
Each case can be checked using if conditions as follows:
1: a[m] > a[m-1] && a[m] > a[m+1]
2: a[m] < a[m-1] && a[m] < a[m+1]
3: a[m] > a[m-1] && a[m] < a[m+1]
4: a[m] < a[m-1] && a[m] > a[m+1]
I am also going to ignore explaining the boundary cases:
The 2nd case is the desired case where a[m] is the local minima
for the 3rd case:
there is always a local minima is to the left, so, set h = m - 1 and continue
for the 4th case:
there is always a local minima to the right, so, l = m + 1 and continue
for the 1st case, I can go on either direction:
I believe this works because the only time you reduce the segment you are considering is when you are sure there is a local minima in the reduced segment, so the segement you search will always have some local minima.
Note: This will work only to find a single local minima and not every local minima. The latter case would require O(n) since you definitely have to see the whole array at least once.