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.
items = [7, 4, 9, 2]
target = 9
position = -1
for i in range(len(items)):
if items[i] == target:
position = i
print(position)- Which indices are checked?
- At which index is the target found?
- Why is
-1useful as an initial value?
Investigate
Use this code to explain exactly how the program works.
names = ["Ava", "Ben", "Cara", "Dan"]
target = "Dan"
found = False
for name in names:
if name == target:
found = True
print(found)- 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
- 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.
numbers = [4, 8, 15, 16, 23]
target = 15
position = -1
for i in range(len(numbers)):
if numbers[i] == target:
position = i
print(position)2
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 Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
Linear search | Python loop through items/indexes | Check each item in order. |
unsorted data allowed | unsorted Python list allowed | No sorting prerequisite. |
target found / not found | position or Boolean flag | A variable can record the result. |
Worked example
This implementation keeps the first matching index and uses -1 for not found.
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)3
- Start at index 0.
- Compare each item with the target.
- 18, 3 and 25 do not match.
- 11 at index 3 matches, so
positionbecomes 3. - The extra
position == -1check 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.
names = ["Nova", "Ari", "Mina", "Jax"]
target = "Mina"
found = False
for name in names:
if name == target:
found = True
print(found)True
- The algorithm still checks items one by one.
- The output records whether any match was found rather than where it was found.
Common mistakes
- Claiming the list must be sorted. Linear search does not require sorting.
- Using Python
inor.index()when the task is specifically asking you to demonstrate the algorithm. - Assuming the target will definitely be present.
Exam-ready checklist
- 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.