Skip to content

Commit eca696b

Browse files
committed
Search In Matrix
1 parent 562dca8 commit eca696b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Matrix/SearchInMatrix.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package SummerTrainingGFG.Matrix;
2+
/**
3+
* @author Vishal Singh
4+
* Sorted Row Wise
5+
* Sorted Column Wise*/
6+
public class SearchInMatrix {
7+
static void search(int[][] arr,int m,int n,int elementToSearch){
8+
int i = 0;
9+
int j = n-1;
10+
while (i < m && j >= 0){
11+
if (arr[i][j] == elementToSearch){
12+
System.out.println("Found at: "+(i+1)+" "+(j+1));
13+
return;
14+
}
15+
else if (arr[i][j] > elementToSearch){
16+
j--;
17+
}
18+
else {
19+
i++;
20+
}
21+
}
22+
System.out.println("Not Found");
23+
}
24+
public static void main(String[] args) {
25+
int[][] arr = {
26+
{10,20,30,40},
27+
{15,25,35,45},
28+
{27,29,37,48},
29+
{32,33,39,50}
30+
};
31+
int elementToSearch = 39;
32+
search(arr,4,4,elementToSearch);
33+
}
34+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Java Solution for Data Structures and Algorithms.
110110

111111
* [Snake Pattern](Matrix/SnakePattern.java)
112112
* [Spiral Traversal](Matrix/SpiralTraversal.java)
113+
* [Search in Matrix Rows Sorted Column Sorted](Matrix/SearchInMatrix.java)
113114

114115
### `NOTE: Raise an issue if any program doesn't work.`
115116
### `More Topics to be added soon`

0 commit comments

Comments
 (0)