Skip to content

Commit 712ada5

Browse files
authored
Correct column pointer initialization in RowColumnWiseSorted2dArrayBinarySearch (#6333)
1 parent ebf5c3d commit 712ada5

File tree

2 files changed

+128
-1
lines changed

2 files changed

+128
-1
lines changed

src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public <T extends Comparable<T>> int[] find(T[][] matrix, T key) {
2929

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

3434
while (rowPointer < matrix.length && colPointer >= 0) {
3535
int comp = target.compareTo(matrix[rowPointer][colPointer]);

src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,131 @@ public void rowColumnSorted2dArrayBinarySearchTestNotFound() {
110110
assertEquals(expected[0], ans[0]);
111111
assertEquals(expected[1], ans[1]);
112112
}
113+
114+
/**
115+
* Tests for a WIDE rectangular matrix (3 rows, 4 columns)
116+
*/
117+
private static final Integer[][] WIDE_RECTANGULAR_MATRIX = {
118+
{10, 20, 30, 40},
119+
{15, 25, 35, 45},
120+
{18, 28, 38, 48},
121+
};
122+
123+
@Test
124+
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixMiddle() {
125+
Integer target = 25; // A value in the middle
126+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
127+
int[] expected = {1, 1};
128+
assertEquals(expected[0], ans[0]);
129+
assertEquals(expected[1], ans[1]);
130+
}
131+
132+
@Test
133+
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixTopRightCorner() {
134+
Integer target = 40; // The top-right corner element
135+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
136+
int[] expected = {0, 3};
137+
assertEquals(expected[0], ans[0]);
138+
assertEquals(expected[1], ans[1]);
139+
}
140+
141+
@Test
142+
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixBottomLeftCorner() {
143+
Integer target = 18; // The bottom-left corner element
144+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
145+
int[] expected = {2, 0};
146+
assertEquals(expected[0], ans[0]);
147+
assertEquals(expected[1], ans[1]);
148+
}
149+
150+
@Test
151+
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixTopLeftCorner() {
152+
Integer target = 10; // The top-left corner element
153+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
154+
int[] expected = {0, 0};
155+
assertEquals(expected[0], ans[0]);
156+
assertEquals(expected[1], ans[1]);
157+
}
158+
159+
@Test
160+
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixBottomRightCorner() {
161+
Integer target = 48; // The bottom-right corner element
162+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
163+
int[] expected = {2, 3};
164+
assertEquals(expected[0], ans[0]);
165+
assertEquals(expected[1], ans[1]);
166+
}
167+
168+
@Test
169+
public void rowColumnSorted2dArrayBinarySearchTestWideMatrixNotFound() {
170+
Integer target = 99; // A value that does not exist
171+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
172+
int[] expected = {-1, -1};
173+
assertEquals(expected[0], ans[0]);
174+
assertEquals(expected[1], ans[1]);
175+
}
176+
177+
/**
178+
* Tests for a TALL rectangular matrix (4 rows, 3 columns)
179+
*/
180+
private static final Integer[][] TALL_RECTANGULAR_MATRIX = {
181+
{10, 20, 30},
182+
{15, 25, 35},
183+
{18, 28, 38},
184+
{21, 31, 41},
185+
};
186+
187+
@Test
188+
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixMiddle() {
189+
Integer target = 28; // A value in the middle
190+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
191+
int[] expected = {2, 1};
192+
assertEquals(expected[0], ans[0]);
193+
assertEquals(expected[1], ans[1]);
194+
}
195+
196+
@Test
197+
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixTopRightCorner() {
198+
Integer target = 30; // The top-right corner element
199+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
200+
int[] expected = {0, 2};
201+
assertEquals(expected[0], ans[0]);
202+
assertEquals(expected[1], ans[1]);
203+
}
204+
205+
@Test
206+
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixBottomLeftCorner() {
207+
Integer target = 21; // The bottom-left corner element
208+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
209+
int[] expected = {3, 0};
210+
assertEquals(expected[0], ans[0]);
211+
assertEquals(expected[1], ans[1]);
212+
}
213+
214+
@Test
215+
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixTopLeftCorner() {
216+
Integer target = 10; // The top-left corner element
217+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
218+
int[] expected = {0, 0};
219+
assertEquals(expected[0], ans[0]);
220+
assertEquals(expected[1], ans[1]);
221+
}
222+
223+
@Test
224+
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixBottomRightCorner() {
225+
Integer target = 41; // The bottom-right corner element
226+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
227+
int[] expected = {3, 2};
228+
assertEquals(expected[0], ans[0]);
229+
assertEquals(expected[1], ans[1]);
230+
}
231+
232+
@Test
233+
public void rowColumnSorted2dArrayBinarySearchTestTallMatrixNotFound() {
234+
Integer target = 5; // A value that does not exist
235+
int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
236+
int[] expected = {-1, -1};
237+
assertEquals(expected[0], ans[0]);
238+
assertEquals(expected[1], ans[1]);
239+
}
113240
}

0 commit comments

Comments
 (0)