Skip to content

Instantly share code, notes, and snippets.

@anil477
Created June 15, 2017 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/6f4e7c76712a7d92127555382d9fe7d2 to your computer and use it in GitHub Desktop.
Save anil477/6f4e7c76712a7d92127555382d9fe7d2 to your computer and use it in GitHub Desktop.
Search in a row wise and column wise sorted matrix
// http://www.geeksforgeeks.org/search-in-row-wise-and-column-wise-sorted-matrix/
class SearchInSortedArray {
public static void search(int[][] a, int r, int c, int k) {
int i = 0,j = c-1;
while ( i < r && j>0)
{
if(a[i][j] == k){
System.out.println(" Found at " + i + " " + j);
return;
}
else if(a[i][j] > k){
j--;
} else {
i++;
}
}
System.out.println("Not Found");
}
public static void main(String[] args) {
int[][] a = new int[][]{
{10, 20, 30, 40},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50},
};
int key = 39;
search(a, 4, 4, key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment