diff --git a/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java b/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java index 3b4b0b083..b40c7554d 100644 --- a/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java @@ -29,7 +29,7 @@ public class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgor public static > 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]); diff --git a/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java index 39ac5bf03..8d1423cbe 100644 --- a/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java @@ -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]); + } }