Where to choose linear search over binary search

user3444063 picture user3444063 · Mar 20, 2014 · Viewed 8.5k times · Source

After having searched the internet I was not able to satisfy myself that I had found a comprehensive set of situations in which a linear search would be preferable to a binary search.

I am essentially wondering whether it would be possible to compile a relatively definitive list of advice (from the point of view of general programming as one might find in industry). Alternatively I would much appreciate it if it could be verified that indeed I have seen all there is to say on the subject.

Answer

user3444063 picture user3444063 · Mar 21, 2014

My list of reasons for choosing a linear search over a binary search are as follows:

  1. The list is unsorted and is only to be searched once

  2. The list is small (though this itself is a vague notion - I've read less than around 100 elements?)

  3. The list will need sorting following the search operation (due to say an insertion), since the resorting will dominate the time complexity of the overall task

  4. The data structure is not random access (like a linked-list)

  5. There is no knowledge of the data that could aid searching (relative proximities?)