mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user