-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrays2D.java
More file actions
61 lines (53 loc) · 1.81 KB
/
Arrays2D.java
File metadata and controls
61 lines (53 loc) · 1.81 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
//import java.util.*;
public class Arrays2D {
public static void searchMatrix(int matrix[][]) { // THIS WORKS FOR NEGATIVE TOOOO!!
int largest = Integer.MIN_VALUE;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] > largest) {
largest = matrix[i][j];
}
}
}
System.out.println(largest);
}
public static boolean staircaseSearch(int matrix[][], int key) {
int row = 0, col = matrix[0].length - 1;
while (row < matrix.length && col >= 0) {
if (key == matrix[row][col]) {
System.out.println("key found 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 = 100;
staircaseSearch(matrix, key);
// int n = matrix.length, m = matrix[0].length;
// Scanner sc = new Scanner(System.in);
// System.out.println("Enter elements of the matrix:");
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// matrix[i][j] = sc.nextInt();
// }
// }
// System.out.println("Matrix is:");
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// System.out.print(matrix[i][j] + " ");
// }
// System.out.println();
// }
// searchMatrix(matrix);
}
}