My algorithm would be like this:
- p=0
- if (A[p] == x) then idx=p and the algorithm is finished else goto next step
- set p += |x-A[p]|
- goto 2.
say A[p] > x. Then, since A items increase or decrease by 1, idx is for sure at least (A[p] - x) index away from p. Same principle goes for A[p] < x.
int p=0, idx=-1;
while (p<len && p>=0)
if (A[p]==x)
idx = p;
else
p+= abs(x-A[p]);
Time complexity: The worst case would be O(n). I expect the average case to be better than O(n) ( I think it's O(log n) but not sure about it). Running time: Definitely better than linear search for all cases.