0
0
DSA C++programming~10 mins

Search in Rotated Sorted Array in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Search in Rotated Sorted Array
Start: Given rotated sorted array and target
Initialize low=0, high=n-1
While low <= high
Calculate mid = (low+high)/2
Check if arr[mid
Return mid
If left half sorted
Target in left?
high=mid-1
Repeat loop
If not found
Return -1
This flow shows how binary search adapts to a rotated sorted array by checking sorted halves and narrowing search.
Execution Sample
DSA C++
int search(vector<int>& nums, int target) {
  int low = 0, high = nums.size() - 1;
  while (low <= high) {
    int mid = low + (high - low) / 2;
    if (nums[mid] == target) return mid;
    if (nums[low] <= nums[mid]) {
      if (nums[low] <= target && target < nums[mid]) high = mid - 1;
      else low = mid + 1;
    } else {
      if (nums[mid] < target && target <= nums[high]) low = mid + 1;
      else high = mid - 1;
    }
  }
  return -1;
}
This code searches for target in a rotated sorted array using modified binary search.
Execution Table
StepOperationlowmidhighCondition CheckedPointer ChangesVisual State
1Initialize pointers0-6--[4,5,6,7,0,1,2]
2Calculate mid036nums[mid]=7 != target=0-[4,5,6,7,0,1,2]
3Check left half sorted036nums[low]=4 <= nums[mid]=7 (True)-[4,5,6,7,0,1,2]
4Check if target in left half0364 <= 0 < 7 (False)low = mid + 1 = 4[4,5,6,7,0,1,2]
5Calculate mid456nums[mid]=1 != target=0-[4,5,6,7,0,1,2]
6Check left half sorted456nums[low]=0 <= nums[mid]=1 (True)-[4,5,6,7,0,1,2]
7Check if target in left half4560 <= 0 < 1 (True)high = mid - 1 = 4[4,5,6,7,0,1,2]
8Calculate mid444nums[mid]=0 == target=0-[4,5,6,7,0,1,2]
9Target found444Return mid=4-[4,5,6,7,0,1,2]
💡 Target found at index 4, loop ends.
Variable Tracker
VariableStartAfter Step 4After Step 7After Step 8Final
low04444
high66444
mid-3544
Key Moments - 3 Insights
Why do we check if the left half is sorted using nums[low] <= nums[mid]?
Because in a rotated sorted array, one half is always sorted. Checking nums[low] <= nums[mid] tells us if the left half is sorted, so we know where to search next (see steps 3 and 6 in execution_table).
Why do we update low or high based on whether the target lies within the sorted half?
If the target is inside the sorted half, we narrow the search to that half by updating low or high accordingly. Otherwise, we search the other half. This logic is shown in steps 4 and 7.
Why do we calculate mid as (low + high) / 2 each iteration?
Because binary search splits the search space in half each time. Updating mid after changing low or high ensures we check the middle of the current search range (see steps 2, 5, and 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'mid' at step 6?
A5
B3
C4
D6
💡 Hint
Check the 'mid' column in execution_table row with Step 6.
At which step does the condition nums[mid] == target become true?
AStep 4
BStep 8
CStep 7
DStep 9
💡 Hint
Look for the step where 'Condition Checked' column shows equality in execution_table.
If the target was 3 instead of 0, what would happen to the 'low' pointer after step 4?
AIt would remain 0
BIt would become 4
CIt would become 3
DIt would become 6
💡 Hint
Check step 4's 'Pointer Changes' and think if target=3 fits in the left half range.
Concept Snapshot
Search in Rotated Sorted Array:
- Use binary search with low, high pointers
- Calculate mid = (low+high)/2
- Check if left half (low to mid) is sorted
- If target in sorted half, narrow search there
- Else search other half
- Repeat until found or low > high
- Return index or -1 if not found
Full Transcript
This concept shows how to find a target number in a rotated sorted array using a modified binary search. We start with two pointers, low and high, at the ends of the array. We find the middle index mid and check if the middle element is the target. If not, we check which half of the array is sorted. If the left half is sorted and the target lies within it, we move the high pointer to mid-1 to search left. Otherwise, we move low to mid+1 to search right. If the right half is sorted, we do the opposite. We repeat this process until we find the target or the pointers cross, meaning the target is not in the array. This method efficiently handles the rotation by always searching the sorted half. The execution table traces each step with pointer values and conditions checked, showing how the search narrows down to the target index.