Homeβ€ΊStudy Resourcesβ€ΊComputer Scienceβ€Ί
How does a linear search work?
Computer Science
Higher

How does a linear search work?

A linear search checks each item in a list in order until it finds the target or reaches the end. Set a found flag to false, loop through the list, and if an item matches, set found to true and record its position. Example: Find 9 in [7, 3, 9, 4]. Index 0: 7 is not 9 Index 1: 3 is not 9 Index 2: 9 matches, so found = true and position = 2 Answer: 9 is found at index 2 after 3 comparisons.

Quick answer: A linear search checks each item in order until it finds the target or reaches the end. It works on an unsorted list.

What the specification expects

Linear search is a standard algorithm in Software design and development in the SQA Higher Computing Science course specification. You need to know:

  • linear search is one of the standard algorithms, with find minimum and maximum, and count occurrences
  • standard algorithms are applied to 1-D arrays or arrays of records
  • you should be able to read and explain code that uses them

Method

  1. Set found to false and the position to -1.
  2. Loop through the list from the first index.
  3. If an item matches the target, set found to true, record the index and stop.
  4. After the loop, report the result.

Worked example 1: target found

Use a linear search to find 9 in [7, 3, 9, 4].

found = False
position = -1
for i in range(len(items)):
    if items[i] == target:
        found = True
        position = i
        break
  1. Set found = false and start at index 0.
  2. Index 0: 7 is not 9. Index 1: 3 is not 9.
  3. Index 2: 9 matches, so found = true and position = 2.

Answer: 9 is found at index 2 after 3 comparisons.

Worked example 2: target not found

Use a linear search to find 5 in [7, 3, 9, 4].

  1. Check index 0: 7. Check index 1: 3.
  2. Check index 2: 9. Check index 3: 4.
  3. The end of the list is reached with no match.

Answer: 5 is not found, after 4 comparisons. found stays false.

Worked example 3: searching text

Find "Bo" in names = [Ana, Bo, Cy].

  1. Index 0: Ana is not Bo.
  2. Index 1: Bo matches.
  3. Stop and record position 1.

Answer: "Bo" is found at index 1 after 2 comparisons.

Worked example 4: worst case

How many comparisons are needed to find 6 in [1, 2, 3, 4, 5, 6]?

  1. The target is the last item.
  2. The search checks 1, 2, 3, 4, 5, then 6.
  3. Every item is checked.

Answer: 6 comparisons, the worst case for a list of 6.

Common mistakes

  • Stopping the loop early with no result, so found is never reported.
  • Not setting found to false before the loop.
  • Assuming the list must be sorted. A linear search works on unsorted lists.

FAQs

Does a linear search need a sorted list?

No. It checks every item in turn, so the order does not matter.

When is a linear search slow?

When the list is long and the target is near the end or missing, because many items are checked.

Want one-to-one help? Find an online Computer Science tutor with ExpertTutor.

Reviewed by

Want one-to-one support?

One-to-one online tuition can help you build confidence in your subject. Find a tutor who teaches your course.

Find a Tutor

Learn face to face in Glasgow

Prefer in-person lessons? Visit the ExpertTutor Glasgow Southside centre.

See the centre