Skip to content

Add tests, remove main in SaddlebackSearch #5674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2024
Merged
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,7 @@
* [RabinKarpAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RabinKarpAlgorithmTest.java)
* [RecursiveBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java)
* [RowColumnWiseSorted2dArrayBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java)
* [SaddlebackSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SaddlebackSearchTest.java)
* [SearchInARowAndColWiseSortedMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java)
* [SortOrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SortOrderAgnosticBinarySearchTest.java)
* [SquareRootBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/SquareRootBinarySearchTest.java)
Expand Down
35 changes: 6 additions & 29 deletions src/main/java/com/thealgorithms/searches/SaddlebackSearch.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.thealgorithms.searches;

import java.util.Scanner;

/**
* Program to perform Saddleback Search Given a sorted 2D array(elements are
* sorted across every row and column, assuming ascending order) of size n*m we
Expand All @@ -27,10 +25,15 @@ private SaddlebackSearch() {
* @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.
* @return The index(row and column) of the element if found. Else returns
* -1 -1.
*/
private static int[] find(int[][] arr, int row, int col, int key) {
static int[] find(int[][] arr, int row, int col, int key) {
if (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) {
Expand All @@ -47,30 +50,4 @@ else if (arr[row][col] > key) {
// else we move right
return find(arr, row, col + 1, key);
}

/**
* Main method
*
* @param args Command line arguments
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int[][] arr;
int i;
int j;
int rows = sc.nextInt();
int col = sc.nextInt();
arr = new int[rows][col];
for (i = 0; i < rows; i++) {
for (j = 0; j < col; j++) {
arr[i][j] = sc.nextInt();
}
}
int ele = sc.nextInt();
// we start from bottom left corner
int[] ans = find(arr, rows - 1, 0, ele);
System.out.println(ans[0] + " " + ans[1]);
sc.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.thealgorithms.searches;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

class SaddlebackSearchTest {

/**
* Test searching for an element that exists in the array.
*/
@Test
void testFindElementExists() {
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, arr.length - 1, 0, 4);
assertArrayEquals(new int[] {0, 3}, result, "Element 4 should be found at (0, 3)");
}

/**
* Test searching for an element that does not exist in the array.
*/
@Test
void testFindElementNotExists() {
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, arr.length - 1, 0, 1000);
assertArrayEquals(new int[] {-1, -1}, result, "Element 1000 should not be found");
}

/**
* Test searching for the smallest element in the array.
*/
@Test
void testFindSmallestElement() {
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, arr.length - 1, 0, -10);
assertArrayEquals(new int[] {0, 0}, result, "Element -10 should be found at (0, 0)");
}

/**
* Test searching for the largest element in the array.
*/
@Test
void testFindLargestElement() {
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, arr.length - 1, 0, 150);
assertArrayEquals(new int[] {4, 4}, result, "Element 150 should be found at (4, 4)");
}

/**
* Test searching in an empty array.
*/
@Test
void testFindInEmptyArray() {
int[][] arr = {};

assertThrows(IllegalArgumentException.class, () -> { SaddlebackSearch.find(arr, 0, 0, 4); });
}

/**
* Test searching in a single element array that matches the search key.
*/
@Test
void testFindSingleElementExists() {
int[][] arr = {{5}};

int[] result = SaddlebackSearch.find(arr, 0, 0, 5);
assertArrayEquals(new int[] {0, 0}, result, "Element 5 should be found at (0, 0)");
}

/**
* Test searching in a single element array that does not match the search key.
*/
@Test
void testFindSingleElementNotExists() {
int[][] arr = {{5}};

int[] result = SaddlebackSearch.find(arr, 0, 0, 10);
assertArrayEquals(new int[] {-1, -1}, result, "Element 10 should not be found in single element array");
}
}