Search in Rotated Sorted Array in Python (LeetCode 33)
Search in Rotated Sorted Array works because one half is always sorted: find it, check if the target is inside. LeetCode 33 in Python.
Search in Rotated Sorted Array asks you to find a value in a sorted list that has been turned: some of its front was moved to the back. It is LeetCode 33: given such a list nums of distinct numbers and a target, return the target's index, or -1 if it is not there, in O(log n) time.

What does a rotated sorted array look like?
Think of a combination lock's wheel. Its digits run 0 to 9 in order. Turn the wheel four clicks and read from the pointer: 4, 5, 6, 7, 8, 9, 0, 1, 2, 3. Nothing is shuffled. The digits are still in order, the order just starts somewhere else and wraps around once.

Plain binary search breaks here. Look at the middle of [4, 5, 6, 7, 8, 9, 0, 1, 2, 3] for 2: the middle is 8, which is bigger than 2, so ordinary binary search would throw the right half away. But 2 is in the right half.
Why is one half always sorted?
The list has exactly one place where the order breaks: the step from 9 down to 0. Cut the list anywhere and that break lands in one piece or the other, never in both. So at least one of the two halves is in order.

That is all binary search needs. You can tell which half is sorted with one comparison: if nums[lo] <= nums[mid], the left half runs in order from nums[lo] to nums[mid]; otherwise the right half runs in order from nums[mid] to nums[hi]. And for a sorted stretch you know its smallest and largest values, so you can tell at once whether the target is inside it. If it is, keep that half. If not, keep the other one.
What does it look like in Python?
The frame is the same as plain binary search: lo and hi mark what's left, and the loop runs until none is left. If the middle is the target, return it. Then:
- left half sorted and the target inside it:
hidrops belowmid; otherwiselojumps past it; - else the right half is sorted: target inside,
lojumps; otherwisehidrops.
This is the program from the video, line for line:
def search(nums, target):
lo, hi = 0, len(nums) - 1
while lo <= hi:
mid = (lo + hi) // 2
if nums[mid] == target:
return mid
if nums[lo] <= nums[mid]:
if nums[lo] <= target < nums[mid]:
hi = mid - 1
else:
lo = mid + 1
else:
if nums[mid] < target <= nums[hi]:
lo = mid + 1
else:
hi = mid - 1
return -1
nums = [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
print(search(nums, 2))
Output:
8
Which numbers does it look at?
Searching the turned lock for 2 takes three looks:
| Look | lo | hi | mid | nums[mid] | Sorted half | Then |
|---|---|---|---|---|---|---|
| 1 | 0 | 9 | 4 | 8 | left, 4 to 8 | no 2 in it: lo = 5 |
| 2 | 5 | 9 | 7 | 1 | right, 1 to 3 | holds 2: lo = 8 |
| 3 | 8 | 9 | 8 | 2 | found: return 8 |
On the first look the left half, 4 to 8, is sorted, and 2 is not between 4 and 8, so the whole left half goes.

On the second look the left piece, 9 to 1, wraps around, so it is not sorted (nums[lo] is 9, bigger than nums[mid], 1). The right piece, 1 to 3, is, and 2 is inside it, so lo jumps to 8.

The third look lands on 2 itself, at index 8.

We also checked search against list.index on every rotation of 3,000 random sorted lists, with targets that are in the list and targets that aren't, and the answers always agree. LeetCode's examples give the expected answers: search([4, 5, 6, 7, 0, 1, 2], 0) returns 4, search([4, 5, 6, 7, 0, 1, 2], 3) returns -1 and search([1], 0) returns -1.
How fast is it?
Every look still throws away half of what's left, so it takes O(log n) time and O(1) extra memory, the same as plain binary search. The only extra work is one comparison per look to find the sorted half. Note the <= in nums[lo] <= nums[mid]: when lo and mid are the same index, that one-element half counts as sorted. This version relies on the numbers being distinct, as LeetCode 33 promises; with repeats (LeetCode 81) you can't always tell which half is sorted.
Try it
Keep the same turned lock, [4, 5, 6, 7, 8, 9, 0, 1, 2, 3], and search for 5 instead. Which index does search return, and how many looks does it take to get there?