0
0
DSA Typescriptprogramming~10 mins

Binary Search vs Linear Search Real Cost Difference in DSA Typescript - Visual Comparison

Choose your learning style9 modes available
Concept Flow - Binary Search vs Linear Search Real Cost Difference
Start Search
Choose Search Type
Linear
Check each
Found?
NoAdjust range
Return
Yes
Return
Adjust range
Repeat
Not Found
Return
Shows the choice between linear and binary search and their step-by-step decision process.
Execution Sample
DSA Typescript
function linearSearch(arr: number[], target: number): number {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) return i;
  }
  return -1;
}

function binarySearch(arr: number[], target: number): number {
  let left = 0, right = arr.length - 1;
  while (left <= right) {
    let 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;
}
Linear search checks each element one by one; binary search divides the search range in half each step.
Execution Table
StepOperationIndex CheckedComparison ResultPointer ChangesVisual State
1Linear Search StartNoneN/Ai=0[5, 8, 12, 20, 25]
2Check arr[0]05 == 20? Noi=1[5, 8, 12, 20, 25]
3Check arr[1]18 == 20? Noi=2[5, 8, 12, 20, 25]
4Check arr[2]212 == 20? Noi=3[5, 8, 12, 20, 25]
5Check arr[3]320 == 20? YesFound at 3[5, 8, 12, 20, 25]
6Linear Search End3Found targetReturn 3[5, 8, 12, 20, 25]
7Binary Search StartNoneN/Aleft=0, right=4[5, 8, 12, 20, 25]
8Calculate mid2arr[2]=12mid=2[5, 8, 12, 20, 25]
9Compare arr[2]212 == 20? Noleft=3, right=4[5, 8, 12, 20, 25]
10Adjust left3left=3left=3, right=4[5, 8, 12, 20, 25]
11Calculate mid3arr[3]=20mid=3[5, 8, 12, 20, 25]
12Compare arr[3]320 == 20? YesFound at 3[5, 8, 12, 20, 25]
13Binary Search End3Found targetReturn 3[5, 8, 12, 20, 25]
14Linear Search Worst Case425 == 30? Noi=5[5, 8, 12, 20, 25]
15Linear Search EndNoneTarget not foundReturn -1[5, 8, 12, 20, 25]
16Binary Search Worst Case StartNoneN/Aleft=0, right=4[5, 8, 12, 20, 25]
17Calculate mid2arr[2]=12mid=2[5, 8, 12, 20, 25]
18Compare arr[2]212 == 30? Noleft=3, right=4[5, 8, 12, 20, 25]
19Calculate mid3arr[3]=20mid=3[5, 8, 12, 20, 25]
20Compare arr[3]320 == 30? Noleft=4, right=4[5, 8, 12, 20, 25]
21Calculate mid4arr[4]=25mid=4[5, 8, 12, 20, 25]
22Compare arr[4]425 == 30? Noleft=4, right=3[5, 8, 12, 20, 25]
23Binary Search EndNoneTarget not foundReturn -1[5, 8, 12, 20, 25]
💡 Search ends when target is found or search range is empty (left > right).
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 7After Step 10After Step 11After Step 14After Step 18After Step 21Final
i (Linear Search)0133 (found)N/AN/AN/A5 (end)N/AN/AN/A
left (Binary Search)N/AN/AN/AN/A0330344
right (Binary Search)N/AN/AN/AN/A4444443
mid (Binary Search)N/AN/AN/AN/A223234N/A
Key Moments - 3 Insights
Why does binary search adjust 'left' or 'right' pointers instead of checking every element?
Binary search divides the search range in half each time by adjusting 'left' or 'right' pointers, reducing the number of checks needed. This is shown in steps 9, 10, 18, and 22 where pointers move instead of checking all elements.
Why does linear search sometimes take more steps than binary search?
Linear search checks each element one by one until it finds the target or reaches the end, as seen in steps 2 to 5 and 14. Binary search quickly narrows down the search range, so it usually takes fewer steps, shown in steps 7 to 13.
What happens when the target is not in the array?
Both searches return -1 when the target is not found. Linear search ends after checking all elements (step 15), binary search ends when 'left' pointer passes 'right' (step 23).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'mid' at step 11 during binary search?
A3
B2
C4
D1
💡 Hint
Check the 'mid (Binary Search)' row in variable_tracker after step 11.
At which step does linear search find the target 20?
AStep 3
BStep 5
CStep 6
DStep 4
💡 Hint
Look at the 'Comparison Result' column in execution_table for linear search steps.
If the target was the first element (5), how would linear search's 'i' variable change in the execution table?
AIt would return at i=0
BIt would check all elements
CIt would return at i=1
DIt would never find the target
💡 Hint
Linear search returns immediately when it finds the target, as shown in steps 2-5.
Concept Snapshot
Binary Search vs Linear Search:
- Linear Search checks each element one by one.
- Binary Search divides search range in half each step.
- Binary Search requires sorted array.
- Linear Search works on any array.
- Binary Search is faster for large sorted arrays.
- Linear Search is simple but slower for big data.
Full Transcript
This visual compares linear and binary search methods. Linear search checks each element from start to end until it finds the target or finishes the list. Binary search works only on sorted arrays and repeatedly checks the middle element, adjusting the search range by moving left or right pointers. The execution table shows step-by-step how each search checks elements and updates pointers. Variable tracker shows how indexes and pointers change. Key moments clarify why binary search adjusts pointers instead of checking all elements, why linear search can take more steps, and what happens when the target is not found. The quiz tests understanding of mid calculation, step when target is found, and behavior if target is first element. The snapshot summarizes the main differences and use cases.