Binary Search vs Linear Search Real Cost Difference in DSA Javascript - Complexity Comparison
We want to understand how searching for a value in a list changes as the list grows.
Which search method works faster when the list gets bigger?
Analyze the time complexity of these two search methods.
// Linear Search
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return i;
}
return -1;
}
// Binary Search (array must be sorted)
function binarySearch(arr, target) {
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 item one by one. Binary search splits the list in half repeatedly to find the target.
Look at what repeats in each method.
- Linear Search primary operation: Checking each element one by one in a loop.
- Linear Search how many times: Up to n times, where n is the list size.
- Binary Search primary operation: Comparing the middle element and cutting the search space in half.
- Binary Search how many times: About log₂ n times, because the list halves each step.
How many checks happen as the list grows?
| Input Size (n) | Linear Search Checks | Binary Search Checks |
|---|---|---|
| 10 | Up to 10 | About 4 |
| 100 | Up to 100 | About 7 |
| 1000 | Up to 1000 | About 10 |
Linear search grows directly with list size. Binary search grows slowly because it halves the list each time.
Time Complexity: O(n) for Linear Search, O(log n) for Binary Search
This means linear search checks every item in the worst case, while binary search quickly narrows down the search by splitting the list.
[X] Wrong: "Binary search works on any list, sorted or not."
[OK] Correct: Binary search only works correctly if the list is sorted, because it relies on dividing the list based on order.
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 interviews and real projects.
"What if the list is sorted but stored as a linked list instead of an array? How would that affect the time complexity of binary search?"
Practice
Solution
Step 1: Understand search methods on sorted lists
Binary Search divides the list repeatedly, reducing search space quickly. Linear Search checks each item one by one.Step 2: Compare efficiency on large sorted lists
Binary Search runs in about log2(1,000,000) ≈ 20 steps, Linear Search may take up to 1,000,000 steps.Final Answer:
Binary Search -> Option AQuick Check:
Binary Search is faster on sorted lists [OK]
- Thinking Linear Search is faster on sorted lists
- Confusing sorted and unsorted list efficiency
- Ignoring logarithmic speed of Binary Search
arr in JavaScript?Solution
Step 1: Calculate middle index correctly
Binary Search finds middle by averaging start and end indices, then flooring to get integer index.Step 2: Check each option for correctness
let mid = Math.floor((start + end) / 2); uses Math.floor to get integer mid index; others either don't floor or use wrong operations.Final Answer:
let mid = Math.floor((start + end) / 2); -> Option DQuick Check:
Floor average of start and end gives mid [OK]
- Not using Math.floor causing float index
- Adding or subtracting indices incorrectly
- Using mid as sum without division
const arr = [2, 4, 6, 8, 10];
function linearSearch(array, target) {
for (let i = 0; i < array.length; i++) {
if (array[i] === target) return i;
}
return -1;
}
console.log(linearSearch(arr, 8));Solution
Step 1: Trace the linear search loop
The function checks each element: arr[0]=2, arr[1]=4, arr[2]=6, arr[3]=8 matches target 8.Step 2: Return index when target found
At index 3, target 8 is found, so function returns 3.Final Answer:
3 -> Option CQuick Check:
Index of 8 in array is 3 [OK]
- Returning value instead of index
- Off-by-one errors in loop
- Confusing index with element value
function binarySearch(arr, target) {
let start = 0;
let end = arr.length;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] < target) start = mid + 1;
else end = mid - 1;
}
return -1;
}Solution
Step 1: Check initialization of end index
Array indices go from 0 to arr.length - 1, so end must be arr.length - 1 to avoid out-of-bounds.Step 2: Understand loop condition and mid usage
With end = arr.length, mid can be arr.length which is invalid index, causing error.Final Answer:
end should be initialized to arr.length - 1 -> Option AQuick Check:
Array last index = length - 1 [OK]
- Setting end to arr.length causing index error
- Changing start incorrectly
- Using wrong loop condition
Solution
Step 1: Analyze search methods on large sorted arrays
Linear Search checks elements one by one, averaging 500,000 comparisons for half the array.Step 2: Understand Binary Search efficiency
Binary Search halves the search space each step, needing about log2(1,000,000) ≈ 20 comparisons.Step 3: Compare average comparisons
20 comparisons (Binary Search) is much less than 500,000 (Linear Search), so Binary Search is better.Final Answer:
Binary Search, because it reduces search space by half each step -> Option BQuick Check:
Binary Search uses log steps, fewer comparisons [OK]
- Choosing Linear Search for sorted arrays
- Ignoring logarithmic speed advantage
- Thinking Binary Search needs sorted array but choosing wrong option
Ready to prove it?
Pass the test and this pattern moves unknown
