- Linear search
In
computer science , linear search is asearch algorithm , also known as sequential search, that is suitable for searching a list of data for a particular value.It operates by checking every element of a list one at a time in sequence until a match is found. Linear search runs in O(n). If the data are distributed randomly, the expected number of comparisons that will be necessary is:
where n is the number of elements in the list and k is the number of times that the value being searched for appears in the list. The best case is that the value is equal to the first element tested, in which case only 1 comparison is needed. The worst case is that the value is not in the list (or it appears only once at the end of the list), in which case n comparisons are needed
The simplicity of the linear search means that if just a few elements are to be searched it is less trouble than more complex methods that require preparation such as sorting the list to be searched or more complex data structures, especially when entries may be subject to frequent revision. Another possibility is when certain values are much more likely to be searched for than others and it can be arranged that such values will be amongst the first considered in the list.
The following pseudocode describes the linear search technique.
For each item in the list: Check to see if the item you're looking for matches the item in the list. If it matches. Return the location where you found it (the index). If it does not match. Continue searching until you reach the end of the list. If we get here, we know the item does not exist in the list. Return -1.
In computer implementations, it is usual to search the list in order, from element 1 to N (or 0 to N - 1, if array indexing starts with zero instead of one) but a slight gain is possible by the reverse order. Suppose an array "A" having elements 1 to N is to be searched for a value "x" and if it is not found, the result is to be zero. for i:=N:1:-1 do %Search from N down to 1. (The step is -1) if A [i] = x then QuitLoop i; next i; Return(i); %Or otherwise employ the value.Implementations of the loop must compare the index value "i" to the final value to decide whether to continue or terminate the loop. If this final value is some variable such "N" then a subtraction "(i - N)" must be done each time, but in going down from "N" the loop termination condition is for a constant, and moreover a special constant. In this case, zero. Most computer hardware allows the sign to be tested, especially the sign of a value in a register, and so execution would be faster. In the case where the loop was for arrays indexed from zero, the loop would be "for i:=N - 1:0:-1 do" and the test on the index variable would be for it negative, not zero.
The pseudocode as written relies on the value of the index variable being available when the for-loop's iteration is exhausted, as being the value it had when the loop condition failed, or a 'QuitLoop' was executed. Some compilers take the position that on exit from a for-loop no such value is defined, in which case it would be necessary to copy the index variable's value to a reporting variable before exiting the loop, or to use another control structure such as a "while" loop, or else explicit code with "go to" statements in pursuit of the fastest-possible execution.
The following code example for the Java programming language is a simple implementation of a linear search.
Wikimedia Foundation. 2010.