RA Hat
RA Hat

Reputation: 25

Linear & Binary search

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

Answers (3)

Md. Mostafizur Rahman
Md. Mostafizur Rahman

Reputation: 481

Here is a great article for Linear vs Binary Search

Upvotes: 0

Ratul Bin Tazul
Ratul Bin Tazul

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

Pritam Sangani
Pritam Sangani

Reputation: 301

It depends. In general, if what you are searching is already sorted - use binary search otherwise use linear search.

Upvotes: 0

Related Questions