Created
June 15, 2017 05:15
-
-
Save anil477/6f4e7c76712a7d92127555382d9fe7d2 to your computer and use it in GitHub Desktop.
Search in a row wise and column wise sorted matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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