0
0
DSA Javascriptprogramming~10 mins

Why Binary Search and What Sorted Order Gives You in DSA Javascript - Why It Works

Choose your learning style9 modes available
Concept Flow - Why Binary Search and What Sorted Order Gives You
Start with sorted array
Set low = 0, high = length-1
Calculate mid = Math.floor((low + high) / 2)
Compare target with array[mid
Found
End
Binary search works by repeatedly dividing a sorted array in half to find a target value quickly.
Execution Sample
DSA Javascript
const arr = [2, 4, 6, 8, 10];
let low = 0;
let high = arr.length - 1;
let target = 6;
while (low <= high) {
  let mid = Math.floor((low + high) / 2);
  if (arr[mid] === target) {
    console.log('Found target at index', mid);
    break;
  } else if (arr[mid] < target) {
    low = mid + 1;
  } else {
    high = mid - 1;
  }
}
This code searches for the number 6 in a sorted array using binary search.
Execution Table
StepOperationlowhighmidarray[mid]ComparisonActionVisual State
1Initialize04---Start search[2, 4, 6, 8, 10]
2Calculate mid04266 == 6Found target[2, 4, 6, 8, 10]
3End-----Stop search[2, 4, 6, 8, 10]
💡 Target found at mid index 2, search ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
lowundefined000
highundefined444
midundefined-22
target6666
Key Moments - 3 Insights
Why must the array be sorted for binary search to work?
Because binary search decides which half to search next by comparing the target to the middle element. If the array is not sorted, this decision is invalid. See execution_table step 2 where mid element comparison guides the search.
What happens if the target is not in the array?
The search keeps adjusting low and high until low > high, meaning the target is not found. The loop ends without finding the target. This is shown by the exit condition in the concept_flow.
Why do we calculate mid as Math.floor((low + high) / 2)?
To find the middle index between low and high, rounding down to avoid fractional index. This ensures we check the middle element correctly, as shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of mid at step 2?
A3
B1
C2
D0
💡 Hint
Check the 'mid' column in execution_table row for step 2.
At which step does the search find the target?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the 'Comparison' and 'Action' columns in execution_table.
If the array was not sorted, what would happen to the binary search process?
AIt might miss the target or give wrong results
BIt would still find the target quickly
CIt would run faster
DIt would search from the end only
💡 Hint
Refer to key_moments about why sorting is necessary.
Concept Snapshot
Binary Search requires a sorted array.
Start with low=0 and high=length-1.
Find mid = Math.floor((low+high)/2).
Compare target with array[mid].
If equal, found target.
If target < array[mid], search left half.
Else, search right half.
Repeat until found or low > high.
Full Transcript
Binary search is a fast way to find a number in a sorted list. We start by looking at the middle number. If it matches the number we want, we stop. If the number we want is smaller, we look only at the left half. If it is bigger, we look at the right half. We keep doing this until we find the number or there is no more list to check. This only works if the list is sorted because we rely on the order to decide which half to check next.