In computer science, linear search or sequential search is a method for finding a particular value in a list, that consists in checking every one of its elements, one at a time and in sequence, until the desired one is found. [1]
Linear search is the the simplest search algorithm; it is a special case of brute-force search. Its worst case cost is proportional to the number of elements in the list; and so is its expected cost, if all list elements are equally likely to be searched for. Therefore, if the list has more than a few elements, other methods (such as binary search or hashing) may be much more efficient
Contents |
Analysis
For a list with n items, the best case is when the value is equal to the first element of the list, in which case only 1 comparison is needed. The worst case is when the value is not in the list (or occurs only once at the end of the list), in which case n comparisons are needed.
If the value being sought occurs k times in the list, and all orderings of the list are equally likely, the expected number of comparisons is
Asymptotically, therefore, the worst-case cost and the expected cost of linear search are both O(n).
Non-uniform probabilities
The performance of linear search improves if the desired value is more likely to be near the beginning of the list than to its end. Therefore, if some values are much more likely to be searched than others, it is desirable to place them at the beginning of the list.
In particular, when the list items are arranged in order of decreasing probability, and these probabilities are geometrically distributed, the cost of linear search is only O(1). If the table size n is large enough, linear search will be faster than binary search, whose cost is O(log n).[1]
Application
Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an unordered list.
When many values have to be searched in the same list, it often pays to pre-process the latter in order to use a faster method. For example, one may sort the list and use binary search, or by build any efficient search data structure from it.
Pseudocode
Forward iteration
The following pseudocode describes a typical variant of linear search, where the result of the search is supposed to be either the location of the list item where the desired value was found; or an invalid location Λ, to indicate that the desired element does not occur in the list.
For each item in the list:
If that item has the desired value,
Stop the search and return the item's location.
Return Λ.
In this pseudocode, the last line is executed only afer all list items have been examined.
If the list is stored as an array data structure, the location may be the index of the item found (usually between 1 and n, or 0 and n−1). In that case the invalid location Λ can be any index before the first element (such as 0 or −1, respectively) or after the last one (n+1 or n, respectively).
If the list is a simply linked list, then the item's location is its reference, and Λ is usually the null pointer.
Recursive version
Linear search can also be described as a recursive algorithm:
If the list is empty, return Λ
Else
If the first item of the list has the desired value, return its location
Else search the value in the remainder of the list, and return the result.
Searching in reverse order
Linear search in an array is usually programmed by stepping up an index variable until it reaches the last index. This normlly requires two comparison instructions for each list item: one to check whether the index has reached the end of the array, and another one to check whether the item has the desired value. In many computers, one can eliminte the first comparison by scanning the items is reverse order
Suppose an array A with elements indexed 0 to n−1 is to be searched for a value x. The following pseudocode performs a forward search, returning n if the value is not found:
i ← 0
repeat this loop:
if i ≥ n exit the loop;
if A[i] = x then exit the loop;
i ← i + 1
return i.
The following pseudocode searches the array in the reverse order, returning −1 when the element is not found:
i ← n
repeat this loop:
i ← i − 1
if i < 0 then exit the loop;
if A[i] = x then exit the loop;
return i.
Most computers have a conditional branch instruction that tests the sign of a value in a register, or the sign of the result of the most recent arithmetic operation. One can use that instruction, which is usually faster than a comparison, to implement the command "if i < 0 then exit the loop".
This optimization is easily implemented when programming in machine or assembly language. That branch instruction is not directly accessible in typical high-level programming languages, although many compilers will be able to perform that optimization on their own.
Using a sentinel
Another way to eliminate the first comparison is to insert the desired item itself as a sentinel value in the list, as in this pseudcode:
A[n] ← x
i ← 0
repeat this loop:
if A[i] = x then exit the loop;
i ← i + 1
return i.
With this stratagem, it is not necessary to check the value of i: even if x was not in A, the loop will terminate when i = n. However this method is viable only if the array element A[n] exists but is not being otherwise used.
Linear search on ordered list
The average performance of linear search can be improved by also using it on an ordered list. In the case of no matching element, the search can terminate at the first element which is greater (lesser) than the unmatched target element, rather than examining the entire list. However, this technique is relevant only for lists that must be accesed sequentially, such as linked lists or files with variable-length records. If the list is stored as an ordered array, then binary search is almost always more efficient than linear search.
See also
References
- ^ a b Donald Knuth (1997). The Art of Computer Programming'. 3: Sorting and Searching (3rd ed.). Addison-Wesley. pp. 396–408. ISBN 0-201-89685-0.
External links
This entry is from Wikipedia, the leading user-contributed encyclopedia. It may not have been reviewed by professional editors (see full disclaimer)

![\begin{cases}
n, & \mbox{if } k = 0 \\[5pt]
\displaystyle\frac{n + 1}{k + 1}, & \mbox{if } 1 \le k \le n
\end{cases}](http://wpcontent.answers.com/math/c/1/6/c16da42d4a6d2ac0ec5052758a131a08.png)



