0
0
DSA Cprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Binary Search as Divide and Conquer
Start with sorted array and target
Find middle element
Compare middle with target
Found target
Search left half
Repeat until found or empty
Binary search repeatedly divides the sorted array in half, comparing the middle element to the target, and narrowing the search to left or right half until the target is found or the search space is empty.
Execution Sample
DSA C
int binarySearch(int arr[], int left, int right, int target) {
  if (right >= left) {
    int mid = left + (right - left) / 2;
    if (arr[mid] == target) return mid;
    if (arr[mid] > target) return binarySearch(arr, left, mid - 1, target);
    return binarySearch(arr, mid + 1, right, target);
  }
  return -1;
}
This code searches for target in sorted array arr using divide and conquer by checking middle and recursing on half.
Execution Table
StepOperationLeft IndexRight IndexMid IndexMid ValueComparisonNext RangeVisual State
1Start search06---0 to 6[2, 4, 6, 8, 10, 12, 14]
2Calculate mid06388 vs 10 (target)4 to 6[2, 4, 6, 8, 10, 12, 14]
3Calculate mid4651212 vs 10 (target)4 to 4[2, 4, 6, 8, 10, 12, 14]
4Calculate mid4441010 vs 10 (target)Found target[2, 4, 6, 8, 10, 12, 14]
5Return index------4
💡 Target found at index 4 on step 4; search ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
left0044-
right6664-
mid-354-
mid_value-81210-
Key Moments - 3 Insights
Why do we calculate mid as left + (right - left) / 2 instead of (left + right) / 2?
Calculating mid as left + (right - left) / 2 prevents integer overflow when left and right are large, as shown in steps 2-4 where mid is safely computed.
Why does the search stop when left > right?
When left > right, the search space is empty, meaning the target is not in the array. This is the base case that returns -1, as shown in step 5.
Why do we recurse on mid - 1 or mid + 1 instead of mid?
Because mid has already been checked and is not the target, we exclude it from the next search range to avoid infinite loops, as seen in steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the mid value at step 3?
A8
B12
C10
D6
💡 Hint
Check the 'Mid Value' column in row for step 3.
At which step does the search range narrow to a single element?
AStep 4
BStep 1
CStep 3
DStep 2
💡 Hint
Look at 'Left Index' and 'Right Index' columns to find when they are equal.
If the target was 7 instead of 10, what would be the next search range after step 2?
A3 to 6
B4 to 6
C0 to 2
D0 to 3
💡 Hint
Compare mid value 8 with target 7 and see which half is searched next in step 2.
Concept Snapshot
Binary Search divides a sorted array by checking the middle element.
If middle equals target, return index.
If target < middle, search left half.
If target > middle, search right half.
Repeat until found or range empty.
Efficient O(log n) search method.
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 the full array and find the middle element. If the middle element equals the target, we return its index. If the target is smaller, we search the left half; if larger, the right half. This process repeats until the target is found or the search range is empty, which means the target is not present. The key steps include calculating the middle index safely to avoid overflow, comparing the middle value with the target, and adjusting the search range accordingly. This divide and conquer approach runs in logarithmic time, making it very efficient for large sorted arrays.