Search code examples
binary-searchlinear-search

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)


Solution

  • 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.