You get a bonus - 1 coin for daily activity. Now you have 1 coin

Linear Search

Lecture



Linear, sequential search — is an algorithm for finding a given value of an arbitrary function on a certain interval. This algorithm is the simplest search algorithm and, unlike, for example, binary search, it imposes no restrictions on the function and has the simplest implementation. The search for the function's value is carried out by simply comparing the next value under consideration (as a rule, the search proceeds from left to right, that is, from smaller argument values to larger ones), and if the values match (to one degree of precision or another), the search is considered complete.

If the interval has length N, then a solution can be found to within Linear Search in time Linear Search. Thus, the asymptotic complexity of the algorithm is — Linear Search. Due to its low efficiency compared to other algorithms, linear search is usually used only when the search interval contains very few elements; nevertheless, linear search requires no additional memory or processing/analysis of the function, so it can operate in streaming mode with data received directly from any source. Likewise, linear search is often used in the form of linear algorithms for finding a maximum/minimum.

As an example, one can consider searching for the value of a function on the set of integers, represented in tabular form.

Contents

  • 1 Example
  • 2 Analysis
  • 3 Applications
  • 4 See also
  • 5 References

Example[edit]

The variables Linear Search and Linear Search contain, respectively, the left and right boundaries of the array segment where the element we need is located. The examination begins with the first element of the segment. If the sought value is not equal to the value of the function at this point, a transition is made to the next point. That is, as a result of each check, the search area decreases by one element.

int function LinearSearch (Array A, int L, int R, int Key);
begin
  for X = L to R do
    if A[X] = Key then
      return X
  return -1; // element not found
end;

Analysis[edit]

For a list of n elements, the best case is one in which the sought value is equal to the first element of the list and only one comparison is required. The worst case occurs when the value is not in the list at all (or it is located at the very end of the list), in which case n comparisons are needed.

If the sought value occurs in the list k times and all occurrences are equally likely, then the expected number of comparisons

Linear Search

For example, if the sought value occurs in the list once, and all occurrences are equally likely, then the average number of comparisons equals Linear Search. However, if it is known that it occurs once, then n — 1 comparisons suffice, and the average number of comparisons will equal

Linear Search

(for n = 2 this number equals 1, which corresponds to a single if-then-else construct).

In any case, the computational complexity of the algorithm is O(n).

Applications[edit]

Linear search is usually very simple to implement and applicable if the list contains few elements, or in the case of a single search in an unordered list.

If it is anticipated that the same list will be searched a large number of times, it often makes sense to preprocess the list, for example by sorting it and subsequently using binary search, or by building some efficient data structure for searching. Frequent modification of the list can also affect the choice of further actions, since it makes it necessary to rebuild the structure.

See also[edit]

  • Binary search
  • Bisection method
  • Golden section method
  • Ternary search
created: 2015-01-08
updated: 2021-04-18
298



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Algorithms"

Terms: Algorithms