21-linear-search.py
python-programming / searching-and-sorting  ·  Lesson 22
Lesson 22 — Searching and sorting

Linear Search

Check items one by one from the beginning until the target is found or the data ends.

Linear search examines each item in order. It can be used on sorted or unsorted data.

Predict

Read the code first. Do not run it yet.

linear-search-predict.py
items = [7, 4, 9, 2]
target = 9
position = -1
for i in range(len(items)):
    if items[i] == target:
        position = i
print(position)
Predict before running
  • Which indices are checked?
  • At which index is the target found?
  • Why is -1 useful as an initial value?

Investigate

Use this code to explain exactly how the program works.

linear-search-investigate.py
names = ["Ava", "Ben", "Cara", "Dan"]
target = "Dan"
found = False
for name in names:
    if name == target:
        found = True
print(found)
Investigate the code
  • How many comparisons are needed in this example?
  • Would the algorithm still work if the names were in a different order?
  • What is the role of the Boolean flag?

The linear-search process

How it works
  • Start at the first item.
  • Compare the current item with the target.
  • If it matches, record that it has been found.
  • Otherwise move to the next item.
  • Stop after a match or after all items have been checked.
linear_search.py
numbers = [4, 8, 15, 16, 23]
target = 15
position = -1

for i in range(len(numbers)):
    if numbers[i] == target:
        position = i

print(position)
Output
2
Understand the stepsThe exact code can vary. The defining feature is checking items one at a time from the start.
Recognise and apply the algorithmBe able to follow these characteristic steps on a data set and recognise the algorithm from program code. Correct implementations can use different variable names or loop structures, so focus on the behaviour rather than memorising one exact version.

Self-study guide

Linear search checks items one at a time from the start of a data set until the target is found or there are no items left to check. It works on unsorted data. OCR requires you to understand the main steps, code segments, prerequisites, how to apply the algorithm, and how to identify it from code.

OCR does not require you to memorise the complete code for the searching and sorting algorithms. The useful skill is recognising the algorithm and being able to apply its steps to a data set.

OCR focusUnderstand, apply and identify linear search. There is no sorted-data prerequisite. Do not treat memorising one exact implementation as the goal.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
Linear searchPython loop through items/indexesCheck each item in order.
unsorted data allowedunsorted Python list allowedNo sorting prerequisite.
target found / not foundposition or Boolean flagA variable can record the result.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

This implementation keeps the first matching index and uses -1 for not found.

lesson-22-worked.py
values = [18, 3, 25, 11, 7, 30]
target = 11
position = -1

for index in range(len(values)):
    if values[index] == target and position == -1:
        position = index

print(position)
Output
3
Work through it line by line
  • Start at index 0.
  • Compare each item with the target.
  • 18, 3 and 25 do not match.
  • 11 at index 3 matches, so position becomes 3.
  • The extra position == -1 check prevents a later duplicate from replacing the first position.

Another example

The same algorithm can use a Boolean found flag when the position is not needed.

lesson-22-example-2.py
names = ["Nova", "Ari", "Mina", "Jax"]
target = "Mina"
found = False

for name in names:
    if name == target:
        found = True

print(found)
Output
True
Why this example matters
  • The algorithm still checks items one by one.
  • The output records whether any match was found rather than where it was found.

Common mistakes

Avoid these errors
  • Claiming the list must be sorted. Linear search does not require sorting.
  • Using Python in or .index() when the task is specifically asking you to demonstrate the algorithm.
  • Assuming the target will definitely be present.
Exam-safe warningFor OCR, understand and apply the algorithm. You are not required to remember one exact code listing.

Exam-ready checklist

You should now be able to
  • State the main steps of linear search.
  • Apply linear search manually to a data set.
  • Identify linear search from code/ERL.
  • Explain that sorted data is not required.
  • Predict the number/order of checks for a given target.

What you have learned

  • Linear search checks items sequentially.
  • It works on sorted or unsorted data.
  • A flag or position variable can record the result.
  • If the target is near the end, many comparisons may be needed.