Correct column pointer initialization in RowColumnWiseSorted2dArrayBinarySearch (#6333)

This commit is contained in:
codingmydna
2025-07-02 21:51:56 +09:00
committed by GitHub
parent ebf5c3d326
commit 712ada5102
2 changed files with 128 additions and 1 deletions

View File

@ -29,7 +29,7 @@ public class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgor
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]);

View File

@ -110,4 +110,131 @@ public class RowColumnWiseSorted2dArrayBinarySearchTest {
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]);
}
}