mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-08-02 14:13:47 +08:00
Add tests, remove main
in SaddlebackSearch
(#5674)
This commit is contained in:
@ -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
|
||||
@ -27,10 +25,15 @@ public final class 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) {
|
||||
@ -47,30 +50,4 @@ public final class SaddlebackSearch {
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user