0
0
DSA Typescriptprogramming

Binary Search vs Linear Search Real Cost Difference in DSA Typescript - Trade-offs & Analysis

Choose your learning style9 modes available
Mental Model
Binary search quickly finds a number by cutting the search area in half each time, while linear search checks each number one by one.
Analogy: Imagine looking for a name in a phone book: linear search is like checking every name from start to end, binary search is like opening the book in the middle and deciding which half to look next.
Array: [1] -> [3] -> [5] -> [7] -> [9] -> [11] -> [13] -> [15] -> null
Linear Search: ↑ starts at index 0
Binary Search: ↑ starts checking middle index
Dry Run Walkthrough
Input: array: [1, 3, 5, 7, 9, 11, 13, 15], search for value 9
Goal: Show how many steps each search takes to find 9 and compare their costs
Step 1: Linear search checks first element (1)
[1↑] -> [3] -> [5] -> [7] -> [9] -> [11] -> [13] -> [15] -> null
Why: Linear search starts from the beginning and checks each element
Step 2: Linear search checks second element (3)
[1] -> [3↑] -> [5] -> [7] -> [9] -> [11] -> [13] -> [15] -> null
Why: Value not found yet, move to next element
Step 3: Linear search checks third element (5)
[1] -> [3] -> [5↑] -> [7] -> [9] -> [11] -> [13] -> [15] -> null
Why: Keep checking sequentially
Step 4: Linear search checks fourth element (7)
[1] -> [3] -> [5] -> [7↑] -> [9] -> [11] -> [13] -> [15] -> null
Why: Still not found, continue
Step 5: Linear search checks fifth element (9) and finds it
[1] -> [3] -> [5] -> [7] -> [9↑] -> [11] -> [13] -> [15] -> null
Why: Found the target value after 5 checks
Step 6: Binary search checks middle element (index 3, value 7)
[1] -> [3] -> [5] -> [7↑] -> [9] -> [11] -> [13] -> [15] -> null
Why: Binary search starts by checking the middle element
Step 7: Since 9 > 7, binary search moves to right half: indexes 4 to 7
[9] -> [11] -> [13] -> [15] with middle at index 5
Why: Discard left half because target is greater than middle
Step 8: Binary search checks middle of right half (index 5, value 11)
[9] -> [11↑] -> [13] -> [15]
Why: Check new middle to narrow down search
Step 9: Since 9 < 11, binary search moves to left half: index 4
[9↑]
Why: Discard right half because target is less than middle
Step 10: Binary search checks index 4 and finds 9
[9↑]
Why: Found the target value after 3 checks
Result:
Linear search final state: 1 -> 3 -> 5 -> 7 -> 9↑ -> 11 -> 13 -> 15 -> null (found after 5 checks)
Binary search final state: 1 -> 3 -> 5 -> 7 -> 9↑ -> 11 -> 13 -> 15 -> null (found after 3 checks)
Annotated Code
DSA Typescript
class Search {
  static linearSearch(arr: number[], target: number): number {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] === target) {
        return i; // found target
      }
    }
    return -1; // not found
  }

  static binarySearch(arr: number[], target: number): number {
    let left = 0;
    let right = arr.length - 1;
    while (left <= right) {
      const mid = Math.floor((left + right) / 2);
      if (arr[mid] === target) {
        return mid; // found target
      } else if (arr[mid] < target) {
        left = mid + 1; // search right half
      } else {
        right = mid - 1; // search left half
      }
    }
    return -1; // not found
  }
}

const arr = [1, 3, 5, 7, 9, 11, 13, 15];
const target = 9;

const linearIndex = Search.linearSearch(arr, target);
console.log(`Linear Search found ${target} at index: ${linearIndex}`);

const binaryIndex = Search.binarySearch(arr, target);
console.log(`Binary Search found ${target} at index: ${binaryIndex}`);
if (arr[i] === target) { return i; }
check each element sequentially until target found
while (left <= right) { const mid = Math.floor((left + right) / 2);
calculate middle index to split search range
if (arr[mid] === target) { return mid; }
check if middle element is target
else if (arr[mid] < target) { left = mid + 1; }
move left pointer to right half if target is greater
else { right = mid - 1; }
move right pointer to left half if target is smaller
OutputSuccess
Linear Search found 9 at index: 4 Binary Search found 9 at index: 4
Complexity Analysis
Time: O(n) for linear search because it may check every element; O(log n) for binary search because it halves the search space each step
Space: O(1) for both because they use only a few variables regardless of input size
vs Alternative: Binary search is much faster on sorted data than linear search, especially for large arrays, but requires the array to be sorted first
Edge Cases
empty array
both searches return -1 immediately because there is nothing to check
DSA Typescript
for (let i = 0; i < arr.length; i++) {
target not in array
both searches return -1 after checking all possible elements or ranges
DSA Typescript
return -1; // not found
array with one element equal to target
both searches find the target immediately at index 0
DSA Typescript
if (arr[i] === target) { return i; }
When to Use This Pattern
When you need to find an item in a sorted list quickly, reach for binary search because it reduces the search area by half each step, unlike linear search which checks one by one.
Common Mistakes
Mistake: Trying binary search on an unsorted array
Fix: Sort the array first or use linear search instead
Mistake: Using wrong mid calculation causing infinite loop
Fix: Calculate mid as Math.floor((left + right) / 2) to avoid errors
Summary
Binary search finds an item by repeatedly dividing the search range in half, while linear search checks each item one by one.
Use binary search when the data is sorted and you want faster search times; use linear search when data is unsorted or small.
The key insight is that binary search cuts the search space quickly, making it much faster than linear search on large sorted lists.