Skip to content

Commit 27a6706

Browse files
Matrix Search element
1 parent af1c105 commit 27a6706

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.java4u.ds.matrix.search;
2+
3+
public class SearchElementInMatrix {
4+
5+
public static void main(String[] args) {
6+
int mat[][] = { { 10, 20, 30, 40 }, { 15, 25, 35, 45 }, { 27, 29, 37, 48 }, { 32, 33, 39, 50 } };
7+
8+
search(mat, 29);
9+
search(mat, 90);
10+
11+
}
12+
13+
// O(n^2) solution
14+
private static void search(int[][] mat, int x) {
15+
boolean found = false;
16+
for (int i = 0; i < mat.length; i++) {
17+
for (int j = 0; j < mat[i].length; j++) {
18+
if (mat[i][j] == x) {
19+
System.out.println("Element found at index ( i , j) : (" + i + "," + j + ")");
20+
found = true;
21+
}
22+
}
23+
}
24+
if (found == false) {
25+
System.out.println("Element not found!!");
26+
}
27+
}
28+
29+
// O(n) solution
30+
private static void search(int[][] mat, int n, int x) {
31+
32+
}
33+
34+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
*
3+
*/
4+
/**
5+
* @author varadago
6+
*
7+
*/
8+
package com.java4u.ds.matrix.search;

0 commit comments

Comments
 (0)