0
0
DSA Javascriptprogramming~10 mins

Search in Rotated Sorted Array in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Search in Rotated Sorted Array
Start with low=0, high=n-1
Calculate mid = (low+high)//2
Check if nums[mid
Return mid
Left half sorted
Check if target in left
Adjust high or low
Repeat until low > high
Return -1 if not found
The search uses a modified binary search to find the target in a rotated sorted array by checking which half is sorted and narrowing the search range accordingly.
Execution Sample
DSA Javascript
function search(nums, target) {
  let low = 0, high = nums.length - 1;
  while (low <= high) {
    let mid = Math.floor((low + high) / 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 a target value in a rotated sorted array using a modified binary search.
Execution Table
StepOperationlowhighmidnums[mid]Check Sorted HalfAdjust low/highSearch RangeResult
1Initialize low and high06----[0..6]-
2Calculate mid0637Left half sorted (nums[0]=4 <= nums[3]=7)Target=3 not in [4..7), set low=mid+1=4[4..6]-
3Calculate mid4651Left half sorted (nums[4]=0 <= nums[5]=1)Target=3 not in [0..1), set low=mid+1=6[6..6]-
4Calculate mid6663Single element, check nums[mid]nums[mid] == target, return 6[6..6]6
5Exit-------Target found at index 6
💡 Search ends when target is found or low > high (not found). Here target found at index 6.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
low04666
high66666
mid-3566
nums[mid]-7133
Key Moments - 3 Insights
Why do we check if nums[low] <= nums[mid] to decide which half is sorted?
Because in a rotated sorted array, one half is always sorted. Checking nums[low] <= nums[mid] tells us if the left half is sorted, helping us decide where to search next (see execution_table step 2).
Why do we adjust low or high based on target's position relative to nums[low], nums[mid], and nums[high]?
We narrow the search to the half where the target can be. If target lies within the sorted half's range, we search there by adjusting low or high accordingly (see execution_table steps 2 and 3).
What happens if the target is not in the array?
The loop continues adjusting low and high until low > high, then returns -1 indicating target not found (not shown in this example but explained in exit_note).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'low' after adjustment?
A4
B6
C5
D3
💡 Hint
Check the 'Adjust low/high' and 'low' columns in execution_table row for step 3.
At which step does the condition low <= high become false if the target is not found?
AAfter step 4
BBefore step 1
CWhen low > high during the loop
DAt step 2
💡 Hint
Refer to the exit_note and loop condition in the code description.
If the target was 5 instead of 3, how would the search range change at step 2?
Ahigh would be set to mid - 1 = 2
Blow would be set to mid + 1 = 4
Clow would remain 0
Dhigh would be set to mid + 1 = 4
💡 Hint
Check the 'Adjust low/high' logic in step 2 for target 3 and think about target 5.
Concept Snapshot
Search in Rotated Sorted Array:
- Use binary search with low, high pointers
- Calculate mid = (low + high) // 2
- Check if nums[mid] == target
- Determine which half is sorted by comparing nums[low] and nums[mid]
- Narrow search to half containing target
- Repeat until found or low > high
- Return index or -1 if not found
Full Transcript
This concept shows how to search for a target value in a rotated sorted array using a modified binary search. We start with low and high pointers at the ends of the array. We calculate mid and check if nums[mid] equals the target. If not, we check which half of the array is sorted by comparing nums[low] and nums[mid]. Depending on which half is sorted and where the target lies, we adjust low or high to narrow the search range. We repeat this until we find the target or low becomes greater than high, meaning the target is not in the array. The execution table traces these steps with variable values and decisions. The variable tracker shows how low, high, mid, and nums[mid] change. Key moments clarify why we check sorted halves and how we adjust pointers. The visual quiz tests understanding of these steps. This method efficiently finds the target in O(log n) time even if the array is rotated.