Skip to content

Commit c2d1dda

Browse files
committed
fix(searches): fix boundary safety and null checks in SaddlebackSearch
1 parent 8b49bc5 commit c2d1dda

2 files changed

Lines changed: 69 additions & 5 deletions

File tree

‎src/main/java/com/thealgorithms/searches/SaddlebackSearch.java‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,39 @@ private SaddlebackSearch() {
1919
}
2020

2121
/**
22-
* This method performs Saddleback Search
22+
* Searches for a key in a row-wise and column-wise sorted 2D array starting from the bottom-left corner.
23+
*
24+
* @param arr The sorted matrix.
25+
* @param key The element to search for.
26+
* @throws IllegalArgumentException if the array is null or empty.
27+
* @return The index (row and column) of the element if found. Else returns {-1, -1}.
28+
*/
29+
public static int[] find(int[][] arr, int key) {
30+
if (arr == null || arr.length == 0) {
31+
throw new IllegalArgumentException("Array is empty");
32+
}
33+
return find(arr, arr.length - 1, 0, key);
34+
}
35+
36+
/**
37+
* This method performs Saddleback Search starting from the specified row and column.
2338
*
2439
* @param arr The **Sorted** array in which we will search the element.
2540
* @param row the current row.
2641
* @param col the current column.
2742
* @param key the element that we want to search for.
28-
* @throws IllegalArgumentException if the array is empty.
43+
* @throws IllegalArgumentException if the array is null or empty.
2944
* @return The index(row and column) of the element if found. Else returns
3045
* -1 -1.
3146
*/
32-
static int[] find(int[][] arr, int row, int col, int key) {
33-
if (arr.length == 0) {
47+
public static int[] find(int[][] arr, int row, int col, int key) {
48+
if (arr == null || arr.length == 0) {
3449
throw new IllegalArgumentException("Array is empty");
3550
}
3651

3752
// array to store the answer row and column
3853
int[] ans = {-1, -1};
39-
if (row < 0 || col >= arr[row].length) {
54+
if (row < 0 || row >= arr.length || arr[row] == null || col < 0 || col >= arr[row].length) {
4055
return ans;
4156
}
4257
if (arr[row][col] == key) {

‎src/test/java/com/thealgorithms/searches/SaddlebackSearchTest.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,53 @@ void testFindSingleElementNotExists() {
8282
int[] result = SaddlebackSearch.find(arr, 0, 0, 10);
8383
assertArrayEquals(new int[] {-1, -1}, result, "Element 10 should not be found in single element array");
8484
}
85+
86+
/**
87+
* Test convenience method searching from the bottom-left corner automatically.
88+
*/
89+
@Test
90+
void testFindWithConvenienceMethod() {
91+
int[][] arr = {{-10, -5, -3, 4, 9}, {-6, -2, 0, 5, 10}, {-4, -1, 1, 6, 12}, {2, 3, 7, 8, 13}, {100, 120, 130, 140, 150}};
92+
int[] result = SaddlebackSearch.find(arr, 4);
93+
assertArrayEquals(new int[] {0, 3}, result, "Element 4 should be found at (0, 3) using convenience overload");
94+
}
95+
96+
/**
97+
* Test searching in a null array throws IllegalArgumentException.
98+
*/
99+
@Test
100+
void testFindWithNullArray() {
101+
assertThrows(IllegalArgumentException.class, () -> SaddlebackSearch.find(null, 4));
102+
assertThrows(IllegalArgumentException.class, () -> SaddlebackSearch.find(null, 0, 0, 4));
103+
}
104+
105+
/**
106+
* Test row index out of bounds returns {-1, -1}.
107+
*/
108+
@Test
109+
void testFindWithRowOutOfBounds() {
110+
int[][] arr = {{1, 2}, {3, 4}};
111+
int[] result = SaddlebackSearch.find(arr, 5, 0, 3);
112+
assertArrayEquals(new int[] {-1, -1}, result, "Row index out of bounds should return {-1, -1}");
113+
}
114+
115+
/**
116+
* Test negative column index returns {-1, -1}.
117+
*/
118+
@Test
119+
void testFindWithNegativeColumn() {
120+
int[][] arr = {{1, 2}, {3, 4}};
121+
int[] result = SaddlebackSearch.find(arr, 1, -1, 3);
122+
assertArrayEquals(new int[] {-1, -1}, result, "Negative column index should return {-1, -1}");
123+
}
124+
125+
/**
126+
* Test null row in matrix returns {-1, -1} safely.
127+
*/
128+
@Test
129+
void testFindWithNullRowInMatrix() {
130+
int[][] arr = {{1, 2}, null, {5, 6}};
131+
int[] result = SaddlebackSearch.find(arr, 1, 0, 5);
132+
assertArrayEquals(new int[] {-1, -1}, result, "Null row in matrix should safely return {-1, -1}");
133+
}
85134
}

0 commit comments

Comments
 (0)