Binary search to find the rotation point in a rotated sorted list
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Consider an array that was initially sorted in ascending order and then rotated at some pivot unknown to you beforehand. To illustrate, an array such as `[5, 6, 7, 1, 2, 3, 4]` is a rotated version of `[1, 2, 3, 4, 5, 6, 7]`, rotated at the pivot index 2. One of the problems arising from such a scenario is finding the rotation point with minimal time complexity. Here, we employ binary search, a powerful algorithm due to its efficiency, to pinpoint this rotation point.
Understanding the Problem Context
The rotation point in a rotated sorted array is the point where the sorted order is interrupted. In the example of the list `[5, 6, 7, 1, 2, 3, 4]`, the rotation point is between 7 and 1. Our task is to identify this rotation index.
Key Observations:
- The array is fully sorted if there was no rotation—that's from the smallest to the largest element.
- The rotation point breaks the array into two subarrays: one with increasing order and one wrapping around.
Binary Search Approach
The classic binary search operates under the principle of divide and conquer, reducing the search space by half each iteration. Here, we adjust the binary search to accommodate the additional complexity introduced by rotation.
Algorithm Steps
- Initialization:
- Start with two pointers: `low` at the beginning of the array and `high` at the end of the array.
- Binary Search Loop:
- Continue the search while `low` is less than `high`.
- Calculate the middle index `mid` using the formula:
- Compare `arr[mid]` with `arr[high]`:
- If `arr[mid] > arr[high]`, this implies that the smallest element (rotation point) lies to the right of `mid`. So, set `low` to `mid + 1`.
- Otherwise, the smallest element is to the left of `mid` or it is `mid` itself, so set `high` to `mid`.
- Conclusion:
- Upon termination, `low` will point to the rotation index (the index of the smallest element).
Example Walkthrough
Consider the array: `[5, 6, 7, 1, 2, 3, 4]`.
- Initial State: `low = 0`, `high = 6`
- First Iteration:
- `mid = 3`. `arr[mid]` is 1, `arr[high]` is 4.
- Since `arr[mid] < arr[high]`, set `high = mid = 3`.
- Second Iteration:
- `mid = 1`. `arr[mid]` is 6, `arr[high]` is 1.
- Since `arr[mid] > arr[high]`, set `low = mid + 1 = 2`.
- Third Iteration:
- `mid = 2`. `arr[mid]` is 7, `arr[high]` is 1.
- Since `arr[mid] > arr[high]`, set `low = mid + 1 = 3`.
Now, `low` equals `high` (both are 3). The rotation point is at index 3.
Complexity Analysis
- Time Complexity: , as the array is halved with each step.
- Space Complexity: , as no additional data structures are used except for a few variables.
Additional Considerations
- Duplicate Elements: If duplicates are allowed in the array, a linear search might be necessary in the worst case, as binary search's efficiency can be undermined.
- Unrotated Array: The algorithm gracefully handles unrotated arrays, returning an index of 0 or the start of such a list.
- Circular Array Handling: The technique is adept not just for rotated sorted arrays, but also for certain circular array problems.
Summary Table
| Action | Condition | Pointer Adjustment |
Move low to the right | arr\[mid] > arr\[high] | low = mid + 1 |
Move high to mid | arr\[mid] \<= arr\[high] | high = mid |
| Rotation index found | When low equals high | Return low |
| Complexity Analysis | Time: Space: |
Employing binary search to deduce the rotation point in a rotated sorted array brings forth an elegant solution leveraging efficiency and algorithmic depth. This technique showcases the versatility of binary search beyond its traditional application to sorted arrays.
Related reading
- Binary Search using start end vs. using start end
- Binary search vs binary search tree
- Binary search with hint
- Binary tree level order traversal
- Binary Space Partitioning Data Structure For Donut 2D Space
- Binary Trees vs. Linked Lists vs. Hash Tables
- Bisecting k-means clustering algorithm explanation
- bisect.insort complexity not as expected

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.