Linear search

Linear search
ClassSearch algorithm
Worst-case performanceO(n)
Best-case performanceO(1)
Average performanceO(n)
Worst-case space complexityO(1) iterative
OptimalYes

In computer science, linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.[1]

A linear search runs in linear time in the worst case, and makes at most n comparisons, where n is the length of the list. If each element is equally likely to be searched, then linear search has an average case of n+1/2 comparisons, but the average case can be affected if the search probabilities for each element vary. Linear search is rarely practical because other search algorithms and schemes, such as the binary search algorithm and hash tables, allow significantly faster searching for all but short lists.[2]

  1. ^ Knuth 1998, §6.1 ("Sequential search").
  2. ^ Knuth 1998, §6.2 ("Searching by Comparison Of Keys").

© MMXXIII Rich X Search. We shall prevail. All rights reserved. Rich X Search