Binary Search vs Linear Search Real Cost Difference in DSA Typescript - Complexity Comparison
We want to understand how searching for a value in a list changes in speed depending on the method used.
Which method is faster as the list grows bigger?
Analyze the time complexity of these two search methods.
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) {
const 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 item one by one. Binary search splits the list and checks the middle repeatedly on a sorted list.
Look at what repeats in each method.
- Linear Search: One loop checking each element from start to end.
- Binary Search: A loop that halves the search space each time by checking the middle element.
- Dominant operation: Comparing elements to the target value.
How many checks happen as the list size grows?
| Input Size (n) | Linear Search Checks | Binary Search Checks |
|---|---|---|
| 10 | Up to 10 | Up to 4 |
| 100 | Up to 100 | Up to 7 |
| 1000 | Up to 1000 | Up to 10 |
Linear search grows directly with list size. Binary search grows slowly because it cuts the list in half each time.
Time Complexity: O(n) for linear search, O(log n) for binary search
Linear search checks each item one by one, so time grows with list size. Binary search quickly narrows down the search, so time grows much slower.
[X] Wrong: "Binary search works on any list, sorted or not."
[OK] Correct: Binary search only works correctly if the list is sorted. Otherwise, it can miss the target or give wrong results.
Knowing when to use linear or binary search shows you understand how to pick the right tool for the job, a key skill in coding and problem solving.
"What if the list is sorted but stored in a linked list instead of an array? How would that affect the time complexity of binary search?"
Practice
Solution
Step 1: Understand the search algorithms
Binary Search divides the list in half repeatedly, while Linear Search checks each element one by one.Step 2: Compare efficiency on large sorted lists
Binary Search takes about log2(1,000,000) ≈ 20 steps, Linear Search takes up to 1,000,000 steps.Final Answer:
Binary Search -> Option CQuick Check:
Binary Search is faster on sorted lists [OK]
- Thinking Linear Search is faster on sorted lists
- Confusing sorted and unsorted list requirements
- Ignoring the difference in step counts
arr for a value target?Solution
Step 1: Check syntax for linear search loop
for (let i = 0; i < arr.length; i++) { if (arr[i] === target) return i; } uses a for loop with index and compares each element to target correctly.Step 2: Analyze other options for correctness
while (arr.includes(target)) { return arr.indexOf(target); } misuses while with includes; arr.findIndex(target); uses findIndex incorrectly; for (let i in arr) { if (arr[i] == target) return i; } uses 'in' which iterates keys but with loose equality.Final Answer:
for (let i = 0; i < arr.length; i++) { if (arr[i] === target) return i; } -> Option BQuick Check:
Correct for loop syntax for linear search [OK]
- Using while loop incorrectly
- Using loose equality (==) instead of strict (===)
- Misusing array methods for search
const arr = [1, 3, 5, 7, 9];
function binarySearch(array: number[], target: number): number {
let left = 0;
let right = array.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (array[mid] === target) return mid;
else if (array[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
console.log(binarySearch(arr, 7));Solution
Step 1: Trace binary search steps for target 7
Initial left=0, right=4, mid=2 (value 5). 5 < 7, so left=3.Step 2: Next iteration mid=3 (value 7), matches target, return 3
The function returns index 3 where 7 is found.Final Answer:
3 -> Option AQuick Check:
Index of 7 in array is 3 [OK]
- Confusing mid calculation
- Returning wrong index
- Not updating left/right correctly
function linearSearch(arr: number[], target: number): number {
for (let i = 0; i <= arr.length; i++) {
if (arr[i] === target) return i;
}
return -1;
}Solution
Step 1: Analyze loop condition
The loop runs while i <= arr.length, which causes out-of-bounds access at i = arr.length.Step 2: Fix loop condition to prevent error
Changing to i < arr.length ensures valid indices only.Final Answer:
Change loop condition to i < arr.length -> Option AQuick Check:
Loop must stay within array bounds [OK]
- Using <= instead of < in loop
- Changing equality operator unnecessarily
- Adding break after return (unreachable code)
Options:
A) Use Linear Search because it checks elements sequentially.
B) Use Linear Search but start from the middle.
C) Use Binary Search but only check every other element.
D) Use Binary Search because it divides the search space each step.
Solution
Step 1: Understand average comparisons for Linear vs Binary Search
Linear Search averages about n/2 = 5,000 comparisons; Binary Search averages about log2(10,000) ≈ 14 comparisons.Step 2: Evaluate options for efficiency
Binary Search halves search space each step, minimizing comparisons. Starting Linear Search from middle or skipping elements breaks correctness or efficiency.Final Answer:
Use Binary Search because it divides the search space each step. -> Option DQuick Check:
Binary Search minimizes comparisons on sorted arrays [OK]
- Thinking linear search is faster on sorted arrays
- Starting linear search from middle doesn't help
- Skipping elements in binary search breaks correctness
Ready to prove it?
Pass the test and this pattern moves unknown
