Consider a sorted array, for example:
[1, 2, 3, 4, 5, 6]
Now, if this array is rotated at some pivot, say at index 3, it would become:
[4, 5, 6, 1, 2, 3]
Notice that the array is still sorted, but it is divided into two parts. Our goal is to search for a target value in such arrays efficiently.
To search in a rotated sorted array, we need to:
class Solution { public static void main(String[] args) { int[] arr = {4, 5, 6, 1, 2, 3}; // Example of rotated sorted array int target = 5; // Searching for the target int result = search(arr, target); // Displaying the result System.out.println("Index of target: " + result); } // Main search function to find the target in a rotated sorted array public static int search(int[] nums, int target) { // Step 1: Find the pivot int pivot = searchPivot(nums); // Step 2: If no pivot, perform regular binary search if (pivot == -1) { return binarySearch(nums, target, 0, nums.length - 1); } // Step 3: If the target is at the pivot, return the pivot index if (nums[pivot] == target) { return pivot; } // Step 4: Decide which half of the array to search if (target >= nums[0]) { return binarySearch(nums, target, 0, pivot - 1); // Search left side } else { return binarySearch(nums, target, pivot + 1, nums.length - 1); // Search right side } } // Binary search helper function static int binarySearch(int[] arr, int target, int start, int end) { while (start <= end) { int mid = start + (end - start) / 2; if (arr[mid] == target) { return mid; // Target found } else if (target < arr[mid]) { end = mid - 1; // Search left half } else { start = mid + 1; // Search right half } } return -1; // Target not found } // Function to find the pivot index in a rotated sorted array static int searchPivot(int[] arr) { int start = 0; int end = arr.length - 1; while (start <= end) { int mid = start + (end - start) / 2; // Check if mid is the pivot point if (mid < end && arr[mid] > arr[mid + 1]) { return mid; } // Check if the pivot is before the mid if (mid > start && arr[mid] < arr[mid - 1]) { return mid - 1; } // Decide whether to move left or right if (arr[mid] <= arr[start]) { end = mid - 1; } else { start = mid + 1; } } return -1; // No pivot found (array is not rotated) } }
search():
binarySearch():
searchPivot():
For an array like [4, 5, 6, 1, 2, 3]:
This method ensures that we search efficiently, achieving a time complexity of O(log n), similar to a standard binary search.
Rotated sorted arrays are a common interview question and a useful challenge to deepen your understanding of binary search. By finding the pivot and adapting our binary search accordingly, we can efficiently search through the array in logarithmic time.
If you found this article helpful, feel free to connect with me on LinkedIn or share your thoughts in the comments! Happy coding!
The above is the detailed content of Building a Rotated Sorted Array Search in Java: Understanding Pivot and Binary Search. For more information, please follow other related articles on the PHP Chinese website!