binary search
rotation point
rotated list
algorithm
data structures

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.

Practice algorithms

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:

  1. The array is fully sorted if there was no rotation—that's from the smallest to the largest element.
  2. 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

  1. Initialization:
    • Start with two pointers: `low` at the beginning of the array and `high` at the end of the array.
  2. Binary Search Loop:
    • Continue the search while `low` is less than `high`.
    • Calculate the middle index `mid` using the formula:
      • mid=low+high−low2mid = low + \frac{high - low}{2}
    • 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`.
  3. 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: O(log⁡n)O(\log n), as the array is halved with each step.
  • Space Complexity: O(1)O(1), 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

ActionConditionPointer Adjustment
Move low to the rightarr\[mid] > arr\[high]low = mid + 1
Move high to midarr\[mid] \<= arr\[high]high = mid
Rotation index foundWhen low equals highReturn low
Complexity AnalysisTime: O(log⁡n)O(\log n) Space: O(1)O(1)

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.


Course
Intermediate
27 lessons
15 hours
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 course
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.

Practice algorithms