Skip to content

Commit 66f76eb

Browse files
authored
Add RotatedBinarySearch (search in rotated sorted array) (#7202)
* feat: implement Smooth Sort algorithm with detailed JavaDoc and test class * style: format LEONARDO array for improved readability with clang-format * feat(sorts): add TournamentSort (winner-tree) * test: add unit test for null array handling in TournamentSort * feat(search): add rotated binary search * test: add unit test for handling middle element in right sorted half of rotated array --------- Co-authored-by: Ahmed Allam <[email protected]>
1 parent 7148661 commit 66f76eb

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.searches;
2+
3+
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4+
5+
/**
6+
* Searches for a key in a sorted array that has been rotated at an unknown pivot.
7+
*
8+
* <p>
9+
* Example:
10+
* {@code [8, 9, 10, 1, 2, 3, 4, 5, 6, 7]}
11+
*
12+
* <p>
13+
* This is a modified binary search. When the array contains no duplicates, the
14+
* time complexity is {@code O(log n)}. With duplicates, the algorithm still
15+
* works but may degrade to {@code O(n)} in the worst case.
16+
*
17+
* @see <a href="https://en.wikipedia.org/wiki/Search_in_rotated_sorted_array">Search in rotated sorted array</a>
18+
* @see SearchAlgorithm
19+
*/
20+
public final class RotatedBinarySearch implements SearchAlgorithm {
21+
22+
@Override
23+
public <T extends Comparable<T>> int find(T[] array, T key) {
24+
int left = 0;
25+
int right = array.length - 1;
26+
27+
while (left <= right) {
28+
int middle = (left + right) >>> 1;
29+
int cmp = key.compareTo(array[middle]);
30+
if (cmp == 0) {
31+
return middle;
32+
}
33+
34+
// Handle duplicates: if we cannot determine which side is sorted.
35+
if (array[left].compareTo(array[middle]) == 0 && array[middle].compareTo(array[right]) == 0) {
36+
left++;
37+
right--;
38+
continue;
39+
}
40+
41+
// Left half is sorted.
42+
if (array[left].compareTo(array[middle]) <= 0) {
43+
if (array[left].compareTo(key) <= 0 && key.compareTo(array[middle]) < 0) {
44+
right = middle - 1;
45+
} else {
46+
left = middle + 1;
47+
}
48+
} else {
49+
// Right half is sorted.
50+
if (array[middle].compareTo(key) < 0 && key.compareTo(array[right]) <= 0) {
51+
left = middle + 1;
52+
} else {
53+
right = middle - 1;
54+
}
55+
}
56+
}
57+
58+
return -1;
59+
}
60+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class RotatedBinarySearchTest {
9+
10+
@Test
11+
void shouldFindElementInRotatedArrayLeftSide() {
12+
RotatedBinarySearch search = new RotatedBinarySearch();
13+
Integer[] array = {8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7};
14+
assertEquals(2, search.find(array, 10));
15+
}
16+
17+
@Test
18+
void shouldFindElementInRotatedArrayRightSide() {
19+
RotatedBinarySearch search = new RotatedBinarySearch();
20+
Integer[] array = {8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7};
21+
assertEquals(6, search.find(array, 2));
22+
}
23+
24+
@Test
25+
void shouldFindElementInNotRotatedArray() {
26+
RotatedBinarySearch search = new RotatedBinarySearch();
27+
Integer[] array = {1, 2, 3, 4, 5, 6, 7};
28+
assertEquals(4, search.find(array, 5));
29+
}
30+
31+
@Test
32+
void shouldReturnMinusOneWhenNotFound() {
33+
RotatedBinarySearch search = new RotatedBinarySearch();
34+
Integer[] array = {4, 5, 6, 7, 0, 1, 2};
35+
assertEquals(-1, search.find(array, 3));
36+
}
37+
38+
@Test
39+
void shouldHandleWhenMiddleIsGreaterThanKeyInRightSortedHalf() {
40+
RotatedBinarySearch search = new RotatedBinarySearch();
41+
Integer[] array = {6, 7, 0, 1, 2, 3, 4, 5};
42+
assertEquals(2, search.find(array, 0));
43+
}
44+
45+
@Test
46+
void shouldHandleDuplicates() {
47+
RotatedBinarySearch search = new RotatedBinarySearch();
48+
Integer[] array = {2, 2, 2, 3, 4, 2};
49+
int index = search.find(array, 3);
50+
assertTrue(index >= 0 && index < array.length);
51+
assertEquals(3, array[index]);
52+
}
53+
}

0 commit comments

Comments
 (0)