diff --git a/src/main/java/com/thealgorithms/searches/BinarySearchStrings.java b/src/main/java/com/thealgorithms/searches/BinarySearchStrings.java new file mode 100644 index 000000000000..ad92953319b6 --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/BinarySearchStrings.java @@ -0,0 +1,81 @@ +package com.thealgorithms.searches; + +/** + * Binary Search implementation specifically for String arrays + * This algorithm finds the position of a target string within a sorted string array + * + * Time Complexity: O(log n * m) where n is array length and m is average string length + * Space Complexity: O(1) + * + * @see Binary Search Algorithm + * @author Jeevan Yewale (https://github.com/JeevanYewale) + */ +public final class BinarySearchStrings { + + private BinarySearchStrings() { + // Utility class + } + + /** + * Performs binary search on a sorted string array + * + * @param array sorted array of strings (must be sorted in lexicographical order) + * @param target the string to search for + * @return index of target string if found, -1 otherwise + */ + public static int search(String[] array, String target) { + if (array == null || array.length == 0 || target == null) { + return -1; + } + + int left = 0; + int right = array.length - 1; + + while (left <= right) { + int mid = left + (right - left) / 2; + int comparison = target.compareTo(array[mid]); + + if (comparison == 0) { + return mid; // Found the target + } else if (comparison < 0) { + right = mid - 1; // Target is in left half + } else { + left = mid + 1; // Target is in right half + } + } + + return -1; // Target not found + } + + /** + * Performs case-insensitive binary search on a sorted string array + * + * @param array sorted array of strings (must be sorted in lexicographical order, case-insensitive) + * @param target the string to search for + * @return index of target string if found, -1 otherwise + */ + public static int searchIgnoreCase(String[] array, String target) { + if (array == null || array.length == 0 || target == null) { + return -1; + } + + int left = 0; + int right = array.length - 1; + String targetLower = target.toLowerCase(); + + while (left <= right) { + int mid = left + (right - left) / 2; + int comparison = targetLower.compareTo(array[mid].toLowerCase()); + + if (comparison == 0) { + return mid; // Found the target + } else if (comparison < 0) { + right = mid - 1; // Target is in left half + } else { + left = mid + 1; // Target is in right half + } + } + + return -1; // Target not found + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/searches/BinarySearchStringsTest.java b/src/test/java/com/thealgorithms/searches/BinarySearchStringsTest.java new file mode 100644 index 000000000000..d94d5ee31f49 --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/BinarySearchStringsTest.java @@ -0,0 +1,56 @@ +package com.thealgorithms.searches; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * Test cases for BinarySearchStrings algorithm + * + * @author Jeevan Yewale (https://github.com/JeevanYewale) + */ +class BinarySearchStringsTest { + + @Test + void testBasicSearch() { + String[] array = {"apple", "banana", "cherry", "date", "elderberry"}; + + assertEquals(0, BinarySearchStrings.search(array, "apple")); + assertEquals(2, BinarySearchStrings.search(array, "cherry")); + assertEquals(4, BinarySearchStrings.search(array, "elderberry")); + assertEquals(-1, BinarySearchStrings.search(array, "grape")); + } + + @Test + void testEmptyArray() { + String[] array = {}; + assertEquals(-1, BinarySearchStrings.search(array, "test")); + } + + @Test + void testNullArray() { + assertEquals(-1, BinarySearchStrings.search(null, "test")); + } + + @Test + void testNullTarget() { + String[] array = {"apple", "banana"}; + assertEquals(-1, BinarySearchStrings.search(array, null)); + } + + @Test + void testSingleElement() { + String[] array = {"single"}; + assertEquals(0, BinarySearchStrings.search(array, "single")); + assertEquals(-1, BinarySearchStrings.search(array, "other")); + } + + @Test + void testCaseInsensitiveSearch() { + String[] array = {"apple", "banana", "cherry", "date", "elderberry"}; + + assertEquals(0, BinarySearchStrings.searchIgnoreCase(array, "APPLE")); + assertEquals(2, BinarySearchStrings.searchIgnoreCase(array, "Cherry")); + assertEquals(-1, BinarySearchStrings.searchIgnoreCase(array, "GRAPE")); + } +} \ No newline at end of file