Skip to content
Open
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
25 changes: 20 additions & 5 deletions src/main/java/com/thealgorithms/searches/SaddlebackSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,39 @@ private SaddlebackSearch() {
}

/**
* This method performs Saddleback Search
* Searches for a key in a row-wise and column-wise sorted 2D array starting from the bottom-left corner.
*
* @param arr The sorted matrix.
* @param key The element to search for.
* @throws IllegalArgumentException if the array is null or empty.
* @return The index (row and column) of the element if found. Else returns {-1, -1}.
*/
public static int[] find(int[][] arr, int key) {
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("Array is empty");
}
return find(arr, arr.length - 1, 0, key);
}

/**
* This method performs Saddleback Search starting from the specified row and column.
*
* @param arr The **Sorted** array in which we will search the element.
* @param row the current row.
* @param col the current column.
* @param key the element that we want to search for.
* @throws IllegalArgumentException if the array is empty.
* @throws IllegalArgumentException if the array is null or empty.
* @return The index(row and column) of the element if found. Else returns
* -1 -1.
*/
static int[] find(int[][] arr, int row, int col, int key) {
if (arr.length == 0) {
public static int[] find(int[][] arr, int row, int col, int key) {
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("Array is empty");
}

// array to store the answer row and column
int[] ans = {-1, -1};
if (row < 0 || col >= arr[row].length) {
if (row < 0 || row >= arr.length || arr[row] == null || col < 0 || col >= arr[row].length) {
return ans;
}
if (arr[row][col] == key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,53 @@ void testFindSingleElementNotExists() {
int[] result = SaddlebackSearch.find(arr, 0, 0, 10);
assertArrayEquals(new int[] {-1, -1}, result, "Element 10 should not be found in single element array");
}

/**
* Test convenience method searching from the bottom-left corner automatically.
*/
@Test
void testFindWithConvenienceMethod() {
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}};
int[] result = SaddlebackSearch.find(arr, 4);
assertArrayEquals(new int[] {0, 3}, result, "Element 4 should be found at (0, 3) using convenience overload");
}

/**
* Test searching in a null array throws IllegalArgumentException.
*/
@Test
void testFindWithNullArray() {
assertThrows(IllegalArgumentException.class, () -> SaddlebackSearch.find(null, 4));
assertThrows(IllegalArgumentException.class, () -> SaddlebackSearch.find(null, 0, 0, 4));
}

/**
* Test row index out of bounds returns {-1, -1}.
*/
@Test
void testFindWithRowOutOfBounds() {
int[][] arr = {{1, 2}, {3, 4}};
int[] result = SaddlebackSearch.find(arr, 5, 0, 3);
assertArrayEquals(new int[] {-1, -1}, result, "Row index out of bounds should return {-1, -1}");
}

/**
* Test negative column index returns {-1, -1}.
*/
@Test
void testFindWithNegativeColumn() {
int[][] arr = {{1, 2}, {3, 4}};
int[] result = SaddlebackSearch.find(arr, 1, -1, 3);
assertArrayEquals(new int[] {-1, -1}, result, "Negative column index should return {-1, -1}");
}

/**
* Test null row in matrix returns {-1, -1} safely.
*/
@Test
void testFindWithNullRowInMatrix() {
int[][] arr = {{1, 2}, null, {5, 6}};
int[] result = SaddlebackSearch.find(arr, 1, 0, 5);
assertArrayEquals(new int[] {-1, -1}, result, "Null row in matrix should safely return {-1, -1}");
}
}
Loading