Given an m ×n matrix in which each row and column is sorted in ascending order, design an algorithm to find an element.

·

3 min read

Algorithm Description:

We are given a Matrix (say A) of size m x n, whose elements are:

$$\begin{pmatrix} a11 & a12 & a13 & ...... & a1n \\ a21 & a22 & a23 & ...... & a2n \\ ...... & ...... & ...... & aij & ...... \\ am1 & am2 & am3 & ...... & amn \end{pmatrix}$$

Let aij or a{i,j} denote the element at position ith row and jth column provided that $$ 1<=i<=m, 1<=j<=n $$

It is given that the Matrix A is sorted in ascending order in both row-wise and the column-wise. Let T be the target element we are searching for.

To solve this, we start with the a1n, we know from our given constraints that everything that is towards the left of a1n, (i.e. a{i=1,j={1,2,...,n-1}}) is less than a{i=1,j=n}, and everything to the bottom of a{i={1,2,...,m-1},j=n} is greater than a{i=1,j=n}

and check if:

  1. current element (i.e. a{1,n}) is equal to T, if True then we found our element and we return the ith row and jth column.
  2. current element < T, then we have to look on the bottom side of a{1,n} i.e we increment i by 1; i.e we update current element to a{2,n}, and we exclude the current row from our effective search space, and reconsider conditions 1,2,3 again.
  3. current element > T, then we have to look on the left side of a{1,n} i.e we decrement j by 1; i.e we update current element to a{i=1,j=n-1}, and we exclude the current column from our effective search space, and reconsider conditions 1,2,3 again

We perform the above 3 checks as long as i and j are with in the boundaries of the M (i.e till 1<= i <=n and 1<= j <=m) if the value of i goes below 1 or value of j goes above n then we can say that T is not present in M.

Base Case:

We observe the best case when T is present exactly at a{1,n}, so we can find it immediately with just one comparison.

Return Value:

The above efficient algorithm returns us the position of T i.e the value of ith row and jth column if T is present in M. Otherwise, returns that the element is not present in M.

Time Complexity:

The time complexity is O(M+N). because we need only one traversal at the most i.e. i from 1 to m and j from n to 1 which is at most M+N steps.

Proof of Correctness:

Our aim is to find the target element T in the given M, if it is present we return it's position in M, else we return T is not present in M. Now, as we are starting from a{1,j}, we know that every element that is towards the left of a{1,j} is smaller than a{1,j}, similarly every element that is to the bottom of the a{1,j} is greater than a{1,j} as per the given constraints on M as per the problem given.

So when we are searching for T, we are efficiently reducing our search space each time when we are making our comparison, and only looking in the newly formed sub search space for T. Those efficient comparisons are points 1,2,3 which are provided in the proposed algorithm. So with each comparison we are either excluding a row, or a column, or evening ending up finding T.

This is done by incrementing the value of i if we want to exclude the current row; and by decrementing the value of j for excluding the current column.

So as we traverse through M, with these efficient ways of comparisons, and increments and decrements on i and j respectively, we will find the T provided that T is in M. If T is not present in T then either i or j value will step out of the boundaries that define M, and when that happens we can declare that T is not present in M.