If you are given a mxn
matrix and k
, a number denoting the row number, how do you check whether the k
th of the matrix is sorted in ascending order?
For example, consider the following matrix:
[1, 2, 3]
[-3, -5, 6]
[7, 8, 9]
When k=1 (first row), the row is sorted in ascending order. When k=2 (second row), the row is not sorted in ascending order.
The important point to note about matrices, is that a matrix is the stacking of one-dimensional arrays.
We can use the algorithm defined in this shot.
The steps of the algorithm are as follows:
k
, is either greater than the number of rows or less than one (the first row), then there is no k
th row in the given matrix.k
th row.i
+1.false
, indicating the row is not sorted.true
, indicating the k
th row of the given matrix is sorted.