-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchinSortedMatrix.java
More file actions
62 lines (51 loc) · 1.69 KB
/
SearchinSortedMatrix.java
File metadata and controls
62 lines (51 loc) · 1.69 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class SearchinSortedMatrix {
public static int binarySearch(int arr[], int key) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] > key) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
public static String binaryMatrixSearch(int mat[][], int key) {
int index = 0;
for (int i = 0; i < mat.length; i++) {
index = binarySearch(mat[i], key);
if (index != -1) {
return "key found at index (" + i + "," + index + ")";
}
}
return "Key not found";
// Time Complexity:O(nlogn)
}
public static String Optimisedsearch(int mat[][], int key) {
int row = 0, col = mat[0].length - 1;
while (row < mat.length && col >= 0) {
if (mat[row][col] == key) {
return "key found at index (" + row + "," + col + ")";
} else if (key < mat[row][col]) {
col--;
} else {
row++;
}
}
return "Key not found";
}
public static void main(String[] args) {
int matrix[][] = { { 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 },
{ 18, 12, 18, 14, 15 },
{ 16, 17, 18, 19, 20 },
{ 21, 22, 23, 24, 25 } };
int key = 18;
System.out.println(binaryMatrixSearch(matrix, key));
System.out.println(Optimisedsearch(matrix, key));
}
}