Back to community
Computer ScienceClass 12 (CBSE) Resolved

How does linear search differ from binary search?

Our algorithms chapter has both linear and binary search and I need to know which is faster and when to use each one. Binary search confuses me.

Asked by Aditya Patil 46 45d ago
Need a Computer Science tutor? Browse verified Computer Science tutors near you.

1 answer

Accepted answer
8

Linear search checks elements one by one from the start until it finds the target or reaches the end, so it works on any list, sorted or not, but is slow for large lists. Binary search is much faster but needs the list to be sorted first. It looks at the middle element; if that is the target, done; if the target is smaller, it searches only the left half; if larger, only the right half, repeatedly halving the search space. Example: in a sorted list of 1000 items, linear search may take up to 1000 comparisons, while binary search takes at most about 10, since it halves each time. So use linear search for small or unsorted data, and binary search for large sorted data where speed matters. Binary search has time complexity O(log n) versus O(n) for linear.

F
Fatima Ansari
355 pts· 45d ago

Sign in as a tutor to answer this doubt.