Algorithm
A linear search sequentially checks each element of the list until it finds an element that matches the target value. If the algorithm 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 followingWith 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) 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,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).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 maySee also
* Ternary search * Hash table * Linear search problemReferences
Citations
Works
* {{DEFAULTSORT:Linear Search Search algorithms