You have to use recursion to solve this problem.
Given a matrix X and number y, you can do binary search for y on the middle row of X and divide the matrix into four parts such that:
A|B
---
C|D
all elements in A are less than y, all elements in D are greater than y, and y can be in B and C. Iteratively find y in B and C.
Since height(A)=height(B)\approx= height(C)=height(D), size(X)>= 2*(size(B)+size(C)) . So the resulting complexity if O(logn).
def find(X,y):
a,b = X.shape
i = a /2
j = binsearch(X[i,:], y)
if X[i,j]==y:
return True
else:
return find( X[ (i+1):a, 0:(j-1)], y ) or find( X[ 0:i, j:b], y )