| 4 | |
| 5 | public class SearchInSorted { |
| 6 | public static boolean staircaseSearch(int matrix[][], int key) { |
| 7 | int n = matrix.length, m = matrix[0].length; |
| 8 | int i = 0, j = n-1; |
| 9 | |
| 10 | while(j>=0 && i<n) { |
| 11 | if(matrix[i][j] == key) { |
| 12 | System.out.println("Found at (" + i +"," + j + ")"); |
| 13 | return true; |
| 14 | } |
| 15 | //go down |
| 16 | else if(matrix[i][j] < key) { |
| 17 | i++; |
| 18 | } |
| 19 | //go left |
| 20 | else if(matrix[i][j] > key) { |
| 21 | j--; |
| 22 | } |
| 23 | |
| 24 | } |
| 25 | |
| 26 | System.out.println("NOT Found"); |
| 27 | return false; |
| 28 | } |
| 29 | public static void main(String args[]) { |
| 30 | int matrix[][] = {{10, 20, 30, 40}, |
| 31 | {15, 25, 35, 45}, |