diff --git a/src/main/java/com/search/SaddlebackSearch.java b/src/main/java/com/search/SaddlebackSearch.java new file mode 100644 index 000000000000..3281d4058d7f --- /dev/null +++ b/src/main/java/com/search/SaddlebackSearch.java @@ -0,0 +1,46 @@ +package com.search; + +/** + * Saddleback search is an algorithm which finds the position of a target value within a 2D sorted array + */ +public class SaddlebackSearch { + + /** + * This method performs Saddleback Search + * + * @param array The **Sorted** array in which we will search the element. + * @param value the element that we want to search for. + * @return The index (row and column) of the element if found. + * Else returns -1 -1. + */ + public static > int[] find(T[][] array, T value) { + return search(array, value, array.length - 1, 0); + } + + /** + * This method performs Saddleback Search + * + * @param array The **Sorted** array in which we will search the element. + * @param value the element that we want to search for. + * @param row the current row. + * @param col the current column. + * @return The index (row and column) of the element if found. + * Else returns -1 -1. + */ + private static > int[] search(T[][] array, T value, int row, int col) { + if (row < 0 || col >= array[row].length) { + return new int[] {-1, -1}; + } + + int compareTo = array[row][col].compareTo(value); + + if (compareTo == 0) { + return new int[] {row, col}; + } else if (compareTo > 0) { + return search(array, value, row - 1, col); + } + + return search(array, value, row, col + 1); + } + +} diff --git a/src/test/java/com/search/SaddlebackSearchTest.java b/src/test/java/com/search/SaddlebackSearchTest.java new file mode 100644 index 000000000000..c236dbd1bf8d --- /dev/null +++ b/src/test/java/com/search/SaddlebackSearchTest.java @@ -0,0 +1,36 @@ +package com.search; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class SaddlebackSearchTest { + + @Test + void testBinarySearch() { + Integer[][] arr1 = { + {1, 2, 3, 4, 5}, + {6, 7, 8, 9, 10}, + {11, 12, 13, 14, 15}, + {16, 17, 18, 19, 20}, + }; + + Assertions.assertArrayEquals(new int[] {1, 0}, SaddlebackSearch.find(arr1, 6), "Incorrect index"); + Assertions.assertArrayEquals(new int[] {1, 4}, SaddlebackSearch.find(arr1, 10), "Incorrect index"); + Assertions.assertArrayEquals(new int[] {3, 4}, SaddlebackSearch.find(arr1, 20), "Incorrect index"); + Assertions.assertArrayEquals(new int[] {0, 0}, SaddlebackSearch.find(arr1, 1), "Incorrect index"); + Assertions.assertArrayEquals(new int[] {-1, -1}, SaddlebackSearch.find(arr1, -10), "Incorrect index"); + + String[][] arr2 = { + {"A", "B", "C", "D", "E"}, + {"F", "G", "H", "I", "J"}, + {"K", "L", "M", "N", "O"}, + }; + + Assertions.assertArrayEquals(new int[] {1, 0}, SaddlebackSearch.find(arr2, "F"), "Incorrect index"); + Assertions.assertArrayEquals(new int[] {1, 4}, SaddlebackSearch.find(arr2, "J"), "Incorrect index"); + Assertions.assertArrayEquals(new int[] {2, 4}, SaddlebackSearch.find(arr2, "O"), "Incorrect index"); + Assertions.assertArrayEquals(new int[] {0, 0}, SaddlebackSearch.find(arr2, "A"), "Incorrect index"); + Assertions.assertArrayEquals(new int[] {-1, -1}, SaddlebackSearch.find(arr2, "X"), "Incorrect index"); + } + +}