Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,27 @@
* doesn't exist in the array, an array
* {-1, -1} will be returned.
*/
public class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgorithm {
public final class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgorithm {

@Override
public <T extends Comparable<T>> int[] find(T[][] matrix, T key) {
return search(matrix, key);
}

public static <T extends Comparable<T>> int[] search(T[][] matrix, T target) {
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0 || target == null) {
return new int[] {-1, -1};
}

int rowPointer = 0; // The pointer at 0th row
int colPointer = matrix[0].length - 1; // The pointer at end column

while (rowPointer < matrix.length && colPointer >= 0) {
if (matrix[rowPointer] == null || colPointer >= matrix[rowPointer].length) {
rowPointer++;
continue;
}

int comp = target.compareTo(matrix[rowPointer][colPointer]);

if (comp == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,60 @@ public void rowColumnSorted2dArrayBinarySearchTestTallMatrixNotFound() {
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void testNullMatrixReturnsNotFound() {
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(null, 5);
int[] expected = {-1, -1};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void testEmptyMatrixReturnsNotFound() {
Integer[][] matrix = {};
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 5);
int[] expected = {-1, -1};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void testEmptyFirstRowReturnsNotFound() {
Integer[][] matrix = {{}};
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 5);
int[] expected = {-1, -1};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void testNullTargetReturnsNotFound() {
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, null);
int[] expected = {-1, -1};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void testMatrixWithNullRow() {
Integer[][] matrix = {
{10, 20, 30},
null,
{18, 28, 38},
};
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 28);
int[] expected = {2, 1};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void testFindMethodThroughInterface() {
RowColumnWiseSorted2dArrayBinarySearch searcher = new RowColumnWiseSorted2dArrayBinarySearch();
int[] ans = searcher.find(WIDE_RECTANGULAR_MATRIX, 35);
int[] expected = {1, 2};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}
}
Loading