Skip to content
Merged
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 @@ -29,7 +29,7 @@ public <T extends Comparable<T>> int[] find(T[][] matrix, T key) {

public static <T extends Comparable<T>> int[] search(T[][] matrix, T target) {
int rowPointer = 0; // The pointer at 0th row
int colPointer = matrix.length - 1; // The pointer at end column
int colPointer = matrix[0].length - 1; // The pointer at end column

while (rowPointer < matrix.length && colPointer >= 0) {
int comp = target.compareTo(matrix[rowPointer][colPointer]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,131 @@ public void rowColumnSorted2dArrayBinarySearchTestNotFound() {
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

/**
* Tests for a WIDE rectangular matrix (3 rows, 4 columns)
*/
private static final Integer[][] WIDE_RECTANGULAR_MATRIX = {
{10, 20, 30, 40},
{15, 25, 35, 45},
{18, 28, 38, 48},
};

@Test
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixMiddle() {
Integer target = 25; // A value in the middle
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
int[] expected = {1, 1};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixTopRightCorner() {
Integer target = 40; // The top-right corner element
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
int[] expected = {0, 3};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixBottomLeftCorner() {
Integer target = 18; // The bottom-left corner element
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
int[] expected = {2, 0};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixTopLeftCorner() {
Integer target = 10; // The top-left corner element
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
int[] expected = {0, 0};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixBottomRightCorner() {
Integer target = 48; // The bottom-right corner element
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
int[] expected = {2, 3};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixNotFound() {
Integer target = 99; // A value that does not exist
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
int[] expected = {-1, -1};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

/**
* Tests for a TALL rectangular matrix (4 rows, 3 columns)
*/
private static final Integer[][] TALL_RECTANGULAR_MATRIX = {
{10, 20, 30},
{15, 25, 35},
{18, 28, 38},
{21, 31, 41},
};

@Test
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixMiddle() {
Integer target = 28; // A value in the middle
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
int[] expected = {2, 1};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixTopRightCorner() {
Integer target = 30; // The top-right corner element
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
int[] expected = {0, 2};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixBottomLeftCorner() {
Integer target = 21; // The bottom-left corner element
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
int[] expected = {3, 0};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixTopLeftCorner() {
Integer target = 10; // The top-left corner element
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
int[] expected = {0, 0};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixBottomRightCorner() {
Integer target = 41; // The bottom-right corner element
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
int[] expected = {3, 2};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}

@Test
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixNotFound() {
Integer target = 5; // A value that does not exist
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
int[] expected = {-1, -1};
assertEquals(expected[0], ans[0]);
assertEquals(expected[1], ans[1]);
}
}
Loading