ArticleslgStudy

computer science

Linear search

Linear search is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Linear search rather than just read about it. In short: In computer science, linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.

Key takeaways

  • Linear search belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Linear search to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Linear search from memory before moving on to harder problems.

Reference excerpt

In computer science, linear search or sequential search is a method for finding an element within a list. 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 the worst case, and makes at most n comparisons, where n is the length of the list. If each element is equally likely to be searched, then linear search has an average case of ⁠n+1/2⁠ comparisons, but the average case can be affected if the search probabilities for each element vary. Linear search is rarely practical because other search algorithms and schemes, such as the binary search algorithm and hash tables, 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 reaches the end of the list, the search terminates unsuccessfully.

Basic algorithm Given a list L of n elements with values or records L0 .... Ln−1, and target value T, the following subroutine uses linear search to find the index of the target T in L.

Set i to 0. If Li = T, the search terminates successfully; return i. Increase i by 1. If i < n, go to step 2. Otherwise, the search terminates unsuccessfully. We can define this in pseudocode as given below, using either an iterative or recursive approach.

function iterativeLinearSearch(list L, T) is for i = 0 to length(L) do if L[i] == T then return i // Return an unsuccessful value (in this case -1). return -1

function recursiveLinearSearch(list L, T, i = 0) is if L[i] == [T] then return i if i > length(L) then return -1 // Unsuccessful value. return recursiveLinearSearch(L, T, i = i + 1)

With a sentinel The basic algorithm above makes two comparisons per iteration: one to check if Li equals T, and the other to check if i still points to a valid index of the list. By adding an extra record Ln 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 i to 0. If Li = T, go to step 4. Increase i by 1 and go to step 2. If i < n, the search terminates successfully; return i. Else, the search terminates unsuccessfully. We can define this in pseudocode as given below, using either an iterative or recursive approach.

function iterativeSentinelSearch(list L, T) is for i = 0 to length(L) do if L[i] == T then if i < length(L) then return i else return -1 return -1

function recursiveSentinelSearch(list L, T, i = 0) is if i >= length(L) then return -1 if L[i] == T then return i return recursiveSentinelSearch(L, T, i = i + 1)

In an ordered table If the list is ordered such that L0 ≤ L1 ... ≤ Ln−1, the search can establish the absence of the target more quickly by concluding the search once Li exceeds the target. This variation requires a sentinel that is greater than the target.

Set i to 0. If Li ≥ T, go to step 4. Increase i by 1 and go to step 2. If Li = T, the search terminates successfully; return i. Else, the search terminates unsuccessfully. We can define this in pseudocode as given below, using either an iterative or recursive approach.

function iterativeTableSearch(list L, T) is for i = 0 to length(L) do if L[i] >= T then if L[i] == T then return i else return -1 return -1

function recursiveTableSearch(list L, T, i = 0) is if i >= length(L) then return -1 if L[i] >= T then if L[i] == T then return i else return -1 return recursiveTableSearch(L, T, i = i + 1)

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

{ n if k = 0 n + 1 k + 1 if 1 ≤ k ≤ n . {\displaystyle {\begin{cases}n&{\mbox{if }}k=0\\[5pt]\displaystyle {\frac {n+1}{k+1}}&{\mbox{if }}1\leq k\leq n.\end{cases}}}

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 n + 1 2 {\displaystyle {\frac {n+1}{2}}} . However, if it is known that it occurs once, then at most n − 1 comparisons are needed, and the expected number of comparisons is

( n + 2 ) ( n − 1 ) 2 n {\displaystyle \displaystyle {\frac {(n+2)(n-1)}{2n}}}

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Linear search

Start with the simplest possible case. Write down what Linear search claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Linear search before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Linear search ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Linear search

In research
Linear search appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Linear search in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Linear search is common in secondary-school and first-year university syllabi. It links to neighbouring topics Search algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Linear search outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Linear search in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Linear search means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Linear search out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Linear search in simple terms?

In computer science, linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.

Why does Linear search matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Linear search?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Linear search.

Tags

  • Search algorithms

Keep exploring