-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchMatrix.java
More file actions
36 lines (30 loc) · 901 Bytes
/
Copy pathSearchMatrix.java
File metadata and controls
36 lines (30 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.*;
public class SearchMatrix {
public static boolean search(int matrix[][], int key){
int row =0;
int col = matrix[0].length-1;
while(row < matrix.length && col>=0){
if(matrix[row] [col] == key ){
System.out.println("found key at("+ row + "," + col + ")");
return true;
}
else if(key < matrix[row][col]){
col--;
}
else{
row++;
}
}
System.out.println("key not found");
return false;
}
public static void main(String [] args){
int matrix[][]= {{10, 20, 30, 40},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50}
};
int key = 33;
search(matrix, key);
}
}