0
0
DSA Goprogramming~10 mins

Binary Search Iterative Approach in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - Binary Search Iterative Approach
Start with sorted array and target
Set low = 0, high = len-1
While low <= high
Calculate mid = (low + high) / 2
Compare array[mid
high=mid-1
Repeat loop
If not found, return -1
This flow shows how binary search narrows down the search range by adjusting low and high pointers until the target is found or the range is empty.
Execution Sample
DSA Go
func binarySearch(arr []int, target int) int {
  low, high := 0, len(arr)-1
  for low <= high {
    mid := (low + high) / 2
    if arr[mid] == target {
      return mid
    } else if arr[mid] < target {
      low = mid + 1
    } else {
      high = mid - 1
    }
  }
  return -1
}
This code searches for target in a sorted array using an iterative binary search and returns the index or -1 if not found.
Execution Table
StepOperationlowhighmidarr[mid]ComparisonActionSearch Range Visual
1Initialize low and high06---Start search[1, 3, 5, 7, 9, 11, 13]
2Calculate mid06377 < 9?arr[mid] < target, low = mid + 1[1, 3, 5, (7), 9, 11, 13]
3Update low46---New range low=4, high=6[9, 11, 13]
4Calculate mid4651111 > 9?arr[mid] > target, high = mid - 1[9, (11), 13]
5Update high44---New range low=4, high=4[9]
6Calculate mid44499 == 9?Found target, return 4[9]
7End4449Target foundReturn index 4[9]
💡 Target found at index 4, search ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
low0044444
high6666444
mid-3-5-44
arr[mid]-7-11-99
Key Moments - 3 Insights
Why do we update 'low' to mid + 1 instead of mid when arr[mid] < target?
Because arr[mid] is less than target and already checked, so we exclude mid by setting low to mid + 1 (see Step 2 and 3 in execution_table).
Why does the loop stop when low > high?
Because the search range becomes empty, meaning target is not in the array. The condition low <= high ensures we only search valid ranges (see Step 7 exit).
Why do we calculate mid as (low + high) / 2 each iteration?
To check the middle element of the current search range and decide which half to search next (see Steps 2, 4, 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the value of 'mid' and 'arr[mid]'?
Amid=4, arr[mid]=9
Bmid=5, arr[mid]=11
Cmid=3, arr[mid]=7
Dmid=6, arr[mid]=13
💡 Hint
Check the 'mid' and 'arr[mid]' columns in row Step 4 of execution_table.
At which step does the condition low <= high become false, ending the loop if target was not found?
AAfter Step 7
BStep 7
CStep 6
DStep 3
💡 Hint
Look at the exit_note and understand the loop condition from concept_flow.
If the target was 14 (not in array), how would the 'low' and 'high' pointers change after Step 6?
Alow would be 4, high would be 4
Blow would be 0, high would be 6
Clow would be 7, high would be 6
Dlow would be 5, high would be 4
💡 Hint
Think about how low moves when arr[mid] < target and when the search ends (see variable_tracker).
Concept Snapshot
Binary Search Iterative Approach:
- Works on sorted arrays
- Use two pointers: low and high
- Calculate mid = (low + high) / 2
- Compare arr[mid] with target
- If equal, return mid
- If arr[mid] < target, move low to mid + 1
- Else, move high to mid - 1
- Repeat until low > high
- Return -1 if not found
Full Transcript
Binary Search Iterative Approach searches a sorted array by repeatedly dividing the search range in half. It starts with low at 0 and high at the last index. Each step calculates mid as the middle index. If the middle element equals the target, it returns the index. If the middle element is less than the target, it moves low to mid + 1 to search the right half. If greater, it moves high to mid - 1 to search the left half. This repeats until low exceeds high, meaning the target is not found. The execution table shows each step's low, high, mid, and comparison results, helping visualize how the search narrows down.