Reputation: 25
What if a computer is superfast and has unlimited memory which searching operation is best to use or can we use any of our choice? (between linear & Binary search)
Upvotes: 0
Views: 316
Reputation: 2151
A linear search scans one item at a time, without jumping to any item. Time complexity is O(n).
Where as a binary search cut down your search to half as soon as you find middle of a sorted list. Time complexity is O(log n).
Note: A binary search will have the same performance as linear search if it's not sorted.
So Binary search is always better no matter how much computing power or space you have.
Upvotes: 0
Reputation: 301
It depends. In general, if what you are searching is already sorted - use binary search otherwise use linear search.
Upvotes: 0