Learn / Binary search

Binary search on rotated arrays

A rotated sorted array is a sorted array cut once and swapped round, like [4, 5, 6, 7, 0, 1, 2]. It is two sorted runs, and the middle always lands in one of them. Compare the middle with an end to learn which half is sorted, and the usual binary search works again.

How to recognise it

  • The array was sorted and then rotated (shifted round) an unknown number of times.
  • The task is to find a value, the smallest value, or how far the array was rotated, faster than O(n).
  • The words "pivot", "rotation point" or "rotated k times" appear in the statement.

The idea in one picture

Take the sorted array [0, 1, 2, 4, 5, 6, 7], cut it after the 2 and put the front part at the back: [4, 5, 6, 7, 0, 1, 2]. Drawn as heights, it climbs, falls once, and climbs again. The only fall is the rotation point. Every question in this pattern is about that one fall.

One half is always sorted

Pick the middle. The single fall lies either in lo..mid or in mid..hi, never in both. So one half is sorted, and one comparison shows which:

CheckMeaning
nums[lo] ≤ nums[mid]the left half lo..mid is sorted
otherwisethe right half mid..hi is sorted

For a sorted half, it is easy to test whether the target lies inside it: compare with its two ends. If it does, search there. If not, search the other half.

Template: search for a target

lo = 0, hi = n - 1
while lo <= hi:
    mid = lo + (hi - lo) / 2
    if nums[mid] == target: return mid
    if nums[lo] <= nums[mid]:                       # left half sorted
        if nums[lo] <= target < nums[mid]: hi = mid - 1
        else: lo = mid + 1
    else:                                           # right half sorted
        if nums[mid] < target <= nums[hi]: lo = mid + 1
        else: hi = mid - 1
return -1

Template: find the minimum (the rotation point)

lo = 0, hi = n - 1
while lo < hi:
    mid = lo + (hi - lo) / 2
    if nums[mid] > nums[hi]: lo = mid + 1   # the fall is after mid
    else: hi = mid                          # mid..hi is sorted: the minimum is mid or before it
return lo                                   # index of the minimum = number of rotations

The index of the minimum is also how many times the array was rotated, and the index just before it (wrapping round) holds the largest value.

Common mistakes

  • Using < instead of ≤ in nums[lo] ≤ nums[mid]. When lo equals mid, the left half is one value and is sorted.
  • Testing only one end of the sorted half, as in target < nums[mid], which sends the search into the wrong half.
  • Comparing with nums[lo] when finding the minimum, which fails on an array that was not rotated.
  • Forgetting that duplicates break the "which half is sorted" test, as in [1, 0, 1, 1, 1].

Problems

0 of 5 solved

Check yourself

In a rotated sorted array with distinct values, why is at least one of lo..mid and mid..hi sorted?

For findMin, nums[mid] > nums[hi]. Where is the minimum?

Why compare nums[mid] with nums[hi] and not with nums[lo] when finding the minimum?

With duplicates, nums[lo] == nums[mid] == nums[hi]. What does the search do?