Search in Rotated Sorted Array in DSA Python - Time & Space Complexity
We want to understand how fast we can find a number in a rotated sorted list.
How does the search time change as the list gets bigger?
Analyze the time complexity of the following code snippet.
def search_rotated(nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
if nums[left] <= nums[mid]:
if nums[left] <= target <= nums[mid]:
right = mid - 1
else:
left = mid + 1
else:
if nums[mid] <= target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return -1
This code searches for a target number in a rotated sorted list using a modified binary search.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that halves the search range each time.
- How many times: The loop runs until the search range is empty, roughly halving the size each time.
Each step cuts the list size roughly in half, so the number of steps grows slowly as the list grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 to 5 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Doubling the list size adds only one extra step.
Time Complexity: O(log n)
This means the search time grows slowly, adding just a few more steps even if the list gets much bigger.
[X] Wrong: "Since the list is rotated, we must check every element, so it takes linear time."
[OK] Correct: The code cleverly uses the sorted parts to skip half the list each time, keeping the search fast.
Understanding this search shows you can adapt classic methods to tricky situations, a skill valued in problem solving.
"What if the list was not rotated but still sorted? How would the time complexity change?"
Practice
Solution
Step 1: Understand normal binary search
Binary search works efficiently on fully sorted arrays by dividing the search space in half each time.Step 2: Identify the challenge with rotated arrays
In a rotated sorted array, the array is sorted but rotated at some pivot, so it is not fully sorted in one direction, making normal binary search invalid without adjustments.Final Answer:
The array is not fully sorted, so normal binary search cannot be directly applied. -> Option CQuick Check:
Rotated array breaks full sorting = normal binary search fails [OK]
- Assuming array is fully sorted
- Ignoring the rotation pivot
- Thinking duplicates cause the main problem
arr with target in Python?Solution
Step 1: Understand array indexing in Python
Python arrays are zero-indexed, so the first element is at index 0 and the last at len(arr) - 1.Step 2: Set search boundaries correctly
To search the entire array, left should start at 0 and right at len(arr) - 1.Final Answer:
left, right = 0, len(arr) - 1 -> Option DQuick Check:
Zero-based indexing means last index is length minus one [OK]
- Starting left at 1 instead of 0
- Setting right to length instead of length - 1
- Confusing inclusive and exclusive ranges
def search_rotated(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
if arr[left] <= arr[mid]:
if arr[left] <= target < arr[mid]:
right = mid - 1
else:
left = mid + 1
else:
if arr[mid] < target <= arr[right]:
left = mid + 1
else:
right = mid - 1
return -1
print(search_rotated([4,5,6,7,0,1,2], 0))Solution
Step 1: Trace the binary search on rotated array
Array: [4,5,6,7,0,1,2], target: 0. Initial left=0, right=6, mid=3 (value 7). Since arr[left] <= arr[mid], left half is sorted. Target 0 is not in [4..7], so move left to mid+1=4.Step 2: Continue search in right half
Now left=4, right=6, mid=5 (value 1). arr[left] <= arr[mid], left half sorted. Target 0 is less than arr[mid], so right=mid-1=4. Next mid=4 (value 0), matches target, return 4.Final Answer:
4 -> Option BQuick Check:
Target 0 found at index 4 [OK]
- Not updating left/right correctly
- Confusing comparison operators
- Returning wrong index or -1 incorrectly
def search(arr, target):
left, right = 0, len(arr) - 1
while left < right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
if arr[left] <= arr[mid]:
if arr[left] <= target < arr[mid]:
right = mid - 1
else:
left = mid + 1
else:
if arr[mid] < target <= arr[right]:
left = mid + 1
else:
right = mid - 1
return -1Solution
Step 1: Analyze loop condition
The loop uses while left < right, which stops when left == right, possibly missing the last element check.Step 2: Correct loop condition for full search
Using while left <= right ensures the search includes the last element, preventing missing the target at the boundary.Final Answer:
The loop condition should be while left <= right, not while left < right. -> Option AQuick Check:
Include last element by using <= in loop condition [OK]
- Using < instead of <= in loop
- Miscomputing mid index
- Returning wrong value when not found
arr = [30, 40, 50, 10, 20], what is the index of the target 10 using an efficient search algorithm?Solution
Step 1: Identify the rotation and search boundaries
Array: [30, 40, 50, 10, 20], target: 10. left=0, right=4, mid=2 (value 50). Left half is sorted (30 to 50).Step 2: Decide which half to search
Target 10 is not in left sorted half (30 to 50), so search right half: left=mid+1=3, right=4. mid=3 (value 10), matches target.Final Answer:
3 -> Option AQuick Check:
Target 10 found at index 3 [OK]
- Assuming array is fully sorted
- Not updating left/right correctly
- Returning wrong index or -1 incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
