mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
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 <60698204+AllamF5J@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||
|
||||
/**
|
||||
* Searches for a key in a sorted array that has been rotated at an unknown pivot.
|
||||
*
|
||||
* <p>
|
||||
* Example:
|
||||
* {@code [8, 9, 10, 1, 2, 3, 4, 5, 6, 7]}
|
||||
*
|
||||
* <p>
|
||||
* 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.
|
||||
*
|
||||
* @see <a href="https://en.wikipedia.org/wiki/Search_in_rotated_sorted_array">Search in rotated sorted array</a>
|
||||
* @see SearchAlgorithm
|
||||
*/
|
||||
public final class RotatedBinarySearch implements SearchAlgorithm {
|
||||
|
||||
@Override
|
||||
public <T extends Comparable<T>> int find(T[] array, T key) {
|
||||
int left = 0;
|
||||
int right = array.length - 1;
|
||||
|
||||
while (left <= right) {
|
||||
int middle = (left + right) >>> 1;
|
||||
int cmp = key.compareTo(array[middle]);
|
||||
if (cmp == 0) {
|
||||
return middle;
|
||||
}
|
||||
|
||||
// Handle duplicates: if we cannot determine which side is sorted.
|
||||
if (array[left].compareTo(array[middle]) == 0 && array[middle].compareTo(array[right]) == 0) {
|
||||
left++;
|
||||
right--;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Left half is sorted.
|
||||
if (array[left].compareTo(array[middle]) <= 0) {
|
||||
if (array[left].compareTo(key) <= 0 && key.compareTo(array[middle]) < 0) {
|
||||
right = middle - 1;
|
||||
} else {
|
||||
left = middle + 1;
|
||||
}
|
||||
} else {
|
||||
// Right half is sorted.
|
||||
if (array[middle].compareTo(key) < 0 && key.compareTo(array[right]) <= 0) {
|
||||
left = middle + 1;
|
||||
} else {
|
||||
right = middle - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RotatedBinarySearchTest {
|
||||
|
||||
@Test
|
||||
void shouldFindElementInRotatedArrayLeftSide() {
|
||||
RotatedBinarySearch search = new RotatedBinarySearch();
|
||||
Integer[] array = {8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7};
|
||||
assertEquals(2, search.find(array, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindElementInRotatedArrayRightSide() {
|
||||
RotatedBinarySearch search = new RotatedBinarySearch();
|
||||
Integer[] array = {8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7};
|
||||
assertEquals(6, search.find(array, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindElementInNotRotatedArray() {
|
||||
RotatedBinarySearch search = new RotatedBinarySearch();
|
||||
Integer[] array = {1, 2, 3, 4, 5, 6, 7};
|
||||
assertEquals(4, search.find(array, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnMinusOneWhenNotFound() {
|
||||
RotatedBinarySearch search = new RotatedBinarySearch();
|
||||
Integer[] array = {4, 5, 6, 7, 0, 1, 2};
|
||||
assertEquals(-1, search.find(array, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldHandleWhenMiddleIsGreaterThanKeyInRightSortedHalf() {
|
||||
RotatedBinarySearch search = new RotatedBinarySearch();
|
||||
Integer[] array = {6, 7, 0, 1, 2, 3, 4, 5};
|
||||
assertEquals(2, search.find(array, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldHandleDuplicates() {
|
||||
RotatedBinarySearch search = new RotatedBinarySearch();
|
||||
Integer[] array = {2, 2, 2, 3, 4, 2};
|
||||
int index = search.find(array, 3);
|
||||
assertTrue(index >= 0 && index < array.length);
|
||||
assertEquals(3, array[index]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user