In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, linear search or sequential search is a method for finding an element within a
list
A list is a Set (mathematics), set of discrete items of information collected and set forth in some format for utility, entertainment, or other purposes. A list may be memorialized in any number of ways, including existing only in the mind of t ...
. It sequentially checks each element of the list until a match is found or the whole list has been searched.
A linear search runs in
linear time
In theoretical computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations ...
in the
worst case, and makes at most comparisons, where is the length of the list. If each element is equally likely to be searched, then linear search has an average case of comparisons, but the average case can be affected if the search probabilities for each element vary. Linear search is rarely practical because other
search algorithm
In computer science, a search algorithm is an algorithm designed to solve a search problem. Search algorithms work to retrieve information stored within particular data structure, or calculated in the Feasible region, search space of a problem do ...
s and schemes, such as the
binary search algorithm
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the ...
and
hash table
In computer science, a hash table is a data structure that implements an associative array, also called a dictionary or simply map; an associative array is an abstract data type that maps Unique key, keys to Value (computer science), values. ...
s, allow significantly faster searching for all but short lists.
Algorithm
A linear search sequentially checks each element of the list until it finds an element that matches the target value. If the
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
reaches the end of the list, the search terminates unsuccessfully.
Basic algorithm
Given a list of elements with values or
records , and target value , the following
subroutine
In computer programming, a function (also procedure, method, subroutine, routine, or subprogram) is a callable unit of software logic that has a well-defined interface and behavior and can be invoked multiple times.
Callable units provide a ...
uses linear search to find the index of the target in .
# Set to 0.
# If , the search terminates successfully; return .
# Increase by 1.
# If , go to step 2. Otherwise, the search terminates unsuccessfully.
With a sentinel
The basic algorithm above makes two comparisons per iteration: one to check if equals ''T'', and the other to check if still points to a valid index of the list. By adding an extra record to the list (a
sentinel value
In computer programming, a sentinel value (also referred to as a flag value, trip value, rogue value, signal value, or dummy data) is a special value in the context of an algorithm which uses its presence as a condition of termination, typically ...
) that equals the target, the second comparison can be eliminated until the end of the search, making the algorithm faster. The search will reach the sentinel if the target is not contained within the list.
# Set to 0.
# If , go to step 4.
# Increase by 1 and go to step 2.
# If , the search terminates successfully; return . Else, the search terminates unsuccessfully.
In an ordered table
If the list is ordered such that , the search can establish the absence of the target more quickly by concluding the search once exceeds the target. This variation requires a sentinel that is greater than the target.
# Set to 0.
# If , go to step 4.
# Increase by 1 and go to step 2.
# If , the search terminates successfully; return . Else, the search terminates unsuccessfully.
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 one 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
:
For example, if the value being sought occurs once in the list, and all orderings of the list are equally likely, the expected number of comparisons is
. However, if it is ''known'' that it occurs once, then at most ''n'' - 1 comparisons are needed, and the expected number of comparisons is
:
(for example, for ''n'' = 2 this is 1, corresponding to a single if-then-else construct).
Either way,
asymptotically 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).
[
]
In general, if items are arranged in order of decreasing probability and the probability of searching for the ''i''th element is
, the expected cost of a single search is
. Under the natural assumption
that the probabilities are not known in advance, or one cannot spend the time to sort the list by probabilities, one can use the
approach of ''self-adjusting data structure'' and move elements towards the head of the list when they are requested in a search.
Two natural heuristics for this self-adjustment are ''Move to Front'' (MF) and ''Transpose'' (T), where the requested element
trades places with its predecessor. It is known that the expected cost of an access in a large sequence of independent accesses,
averaged over all initial orders of the list, satisfies
.
In terms of
amortized cost, averaging over a worst-case sequence of operations (note - among sequences satisfying the assumption on probabilities), we have
, while
can be as bad as
.
[
]
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 un-ordered list.
When many values have to be searched in the same list, it often pays to pre-process the list in order to use a faster method. For example, one may
sort the list and use
binary search
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the m ...
, or build an efficient
search data structure from it. Should the content of the list change frequently, repeated re-organization may be more trouble than it is worth.
As a result, even though in theory other search algorithms may be faster than linear search (for instance
binary search
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the m ...
), in practice even on medium-sized arrays (around 100 items or less) it might be infeasible to use anything else. On larger arrays, it only makes sense to use other, faster search methods if the data is large enough, because the initial time to prepare (sort) the data is comparable to many linear searches.
See also
*
Ternary search
*
Hash table
In computer science, a hash table is a data structure that implements an associative array, also called a dictionary or simply map; an associative array is an abstract data type that maps Unique key, keys to Value (computer science), values. ...
*
Linear search problem
References
Citations
Works
*
{{DEFAULTSORT:Linear Search
Search algorithms