From c08c7eb438c4b37f37237efac23af693f169a890 Mon Sep 17 00:00:00 2001 From: Soham Date: Tue, 29 Sep 2026 21:57:55 +0530 Subject: [PATCH] Improve rotated binary search edge cases --- .../searches/RotatedBinarySearch.java | 12 ++++- .../searches/RotatedBinarySearchTest.java | 46 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/RotatedBinarySearch.java b/src/main/java/com/thealgorithms/searches/RotatedBinarySearch.java index 86099b2fa2fa..4b9717c20fe4 100644 --- a/src/main/java/com/thealgorithms/searches/RotatedBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RotatedBinarySearch.java @@ -12,7 +12,13 @@ *

* This is a modified binary search. When the array contains no duplicates, the * time complexity is {@code O(log n)}. With duplicates, the algorithm still - * works but may degrade to {@code O(n)} in the worst case. + * works but may degrade to {@code O(n)} in the worst case. The algorithm uses + * {@code O(1)} additional space and returns {@code -1} for null or empty input, + * or when the key is not present. + * + * @param array sorted array rotated at an unknown pivot + * @param key value to search for + * @return an index containing {@code key}, or {@code -1} if it is not present * * @see Search in rotated sorted array * @see SearchAlgorithm @@ -21,6 +27,10 @@ public final class RotatedBinarySearch implements SearchAlgorithm { @Override public > int find(T[] array, T key) { + if (array == null || array.length == 0 || key == null) { + return -1; + } + int left = 0; int right = array.length - 1; diff --git a/src/test/java/com/thealgorithms/searches/RotatedBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RotatedBinarySearchTest.java index 1e6ab4c37fcc..f18a0805eb22 100644 --- a/src/test/java/com/thealgorithms/searches/RotatedBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/RotatedBinarySearchTest.java @@ -50,4 +50,50 @@ void shouldHandleDuplicates() { assertTrue(index >= 0 && index < array.length); assertEquals(3, array[index]); } + + @Test + void shouldReturnMinusOneForNullArray() { + RotatedBinarySearch search = new RotatedBinarySearch(); + assertEquals(-1, search.find(null, 1)); + } + + @Test + void shouldReturnMinusOneForEmptyArray() { + RotatedBinarySearch search = new RotatedBinarySearch(); + assertEquals(-1, search.find(new Integer[0], 1)); + } + + @Test + void shouldFindElementInSingleElementArray() { + RotatedBinarySearch search = new RotatedBinarySearch(); + assertEquals(0, search.find(new Integer[] {7}, 7)); + } + + @Test + void shouldReturnMinusOneWhenSingleElementArrayDoesNotContainKey() { + RotatedBinarySearch search = new RotatedBinarySearch(); + assertEquals(-1, search.find(new Integer[] {7}, 8)); + } + + @Test + void shouldFindElementAtRotationPivot() { + RotatedBinarySearch search = new RotatedBinarySearch(); + Integer[] array = {4, 5, 6, 7, 0, 1, 2}; + assertEquals(4, search.find(array, 0)); + } + + @Test + void shouldFindElementsAtBothEnds() { + RotatedBinarySearch search = new RotatedBinarySearch(); + Integer[] array = {4, 5, 6, 7, 0, 1, 2}; + assertEquals(0, search.find(array, 4)); + assertEquals(6, search.find(array, 2)); + } + + @Test + void shouldReturnMinusOneForMissingKeyInAllDuplicatesArray() { + RotatedBinarySearch search = new RotatedBinarySearch(); + Integer[] array = {2, 2, 2, 2, 2, 2, 2, 2}; + assertEquals(-1, search.find(array, 3)); + } }