I posed this problem as homework last semester, and two students, who I considered average, surprised me by coming up with a very elegant, simple and (possibly) optimal algorithm:
Find(k, tab, x, y) let m = tab[x][y] if k = m then return "Found" else if k > m then return Find(k, tab, x, y + 1) else return Find(k, tab, x - 1, y)
This algorithm eliminates either one row or one column for each call (note that it is a tail recursive and can be converted to a loop, thereby avoiding recursive calls). So, if your matrix is ββn * m, the algorithm executes in O (n + m). This solution is better than a dichotomous search backing off (which is the solution I expected when distributing this problem).
EDIT : I fixed the typo (k became x in recursive calls), and also, as Chris noted, this should initially be called with the upper right corner, i.e. Find (k, tab, n, 1), where n is the number of lines.
source share