0
0
DSA Typescriptprogramming~10 mins

Binary Search as Divide and Conquer in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Binary Search as Divide and Conquer
Start with sorted array and target
Calculate middle index
Compare middle element with target
Equal?
Found
End Repeat Repeat
Binary search splits the sorted array in half each time, comparing the middle element to the target, then searching left or right half accordingly until found or empty.
Execution Sample
DSA Typescript
function binarySearch(arr: number[], target: number): number {
  let left = 0, right = arr.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) return mid;
    else if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
  }
  return -1;
}
This code searches for a target number in a sorted array by repeatedly dividing the search range in half.
Execution Table
StepOperationLeft IndexRight IndexMiddle IndexMiddle ValueComparisonActionArray State
1Initialize06---Start search[1, 3, 5, 7, 9, 11, 13]
2Calculate mid06377 vs 97 < 9, move left to mid+1[1, 3, 5, 7, 9, 11, 13]
3Update left46---Search right half[1, 3, 5, 7, 9, 11, 13]
4Calculate mid4651111 vs 911 > 9, move right to mid-1[1, 3, 5, 7, 9, 11, 13]
5Update right44---Search left half[1, 3, 5, 7, 9, 11, 13]
6Calculate mid44499 vs 9Found target at index 4[1, 3, 5, 7, 9, 11, 13]
7Return-----Return index 4[1, 3, 5, 7, 9, 11, 13]
💡 Target found at index 4, search ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
left0044444
right6664444
mid-3-5-4-
middleValue-7-11-9-
Key Moments - 3 Insights
Why do we update 'left' to mid + 1 when middle value is less than target?
Because the target cannot be at mid or any index less than mid, so we move 'left' to mid + 1 to search the right half, as shown in execution_table step 3.
Why do we stop when left > right?
When 'left' passes 'right', it means the search space is empty and the target is not found. This condition stops the loop, but in this example, the target was found before that (step 6).
Why do we calculate mid as Math.floor((left + right) / 2)?
To find the middle index between left and right, rounding down to avoid fractional index, ensuring we pick a valid array position each time (seen in steps 2, 4, 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'mid' at step 4?
A4
B3
C5
D6
💡 Hint
Check the 'Middle Index' column at step 4 in the execution_table.
At which step does the search range narrow to a single element?
AStep 5
BStep 6
CStep 3
DStep 2
💡 Hint
Look at 'Left Index' and 'Right Index' columns to find when they become equal.
If the target was 12 instead of 9, what would happen at step 4?
AReturn index 5
BMove left to mid + 1
CMove right to mid - 1
DReturn -1 immediately
💡 Hint
Compare middle value 11 with target 12 and see the action in step 4.
Concept Snapshot
Binary Search divides a sorted array by half each step.
Calculate middle index: mid = floor((left + right)/2).
Compare middle value with target.
If equal, return mid index.
If target > mid value, search right half (left = mid + 1).
If target < mid value, search left half (right = mid - 1).
Stop when left > right (target not found).
Full Transcript
Binary search is a method to find a target value in a sorted array by repeatedly dividing the search range in half. We start with left at 0 and right at the last index. We calculate the middle index and compare the middle element with the target. If they are equal, we return the middle index. If the target is greater, we move the left pointer to mid + 1 to search the right half. If the target is smaller, we move the right pointer to mid - 1 to search the left half. This process repeats until the target is found or the search range is empty (left > right). The execution table shows each step with pointer updates and comparisons, helping visualize how the search narrows down. The variable tracker shows how left, right, mid, and middle value change over time. Key moments clarify why pointers move and when the search stops. The visual quiz tests understanding of these steps.