Edexcel International Advanced Level Computer Science, point 10.3.1
This is the coding side of the linear search in 5.2.1. In Unit 2 you may need to amend and write a linear search, choosing the right version for the situation.
a. Worst-case search of an unsorted list
Check every item, no early exit. It always does comparisons.
def linear_search_worst(items, target):
found_at = -1
for i in range(len(items)):
if items[i] == target:
found_at = i
return found_at
This keeps looking even after finding the target. It finds the last match, and always does comparisons: the worst case every time. It is inefficient, but useful for counting how many comparisons a worst-case search needs, or finding the last occurrence.
b. Early exit when found (unsorted list)
Stop as soon as the target is found:
def linear_search(items, target):
for i in range(len(items)):
if items[i] == target:
return i # exit immediately
return -1 # not found
- Best case: found first (1 comparison). Worst case: last or missing ( comparisons). Average: about .
returnends the function, which is the early exit.- In an unsorted list, you can only say it is missing after checking all items.
Using a flag and a while loop (an alternative form that an algorithm may specify):
def linear_search_while(items, target):
i = 0
found = False
while i < len(items) and not found:
if items[i] == target:
found = True
else:
i = i + 1
return i if found else -1
c. Early exit when found on a sorted list
On a sorted list the early exit when found works exactly the same as for an unsorted list (b). The benefit of sorted data comes in (d).
d. Early exit when not in the list (sorted list)
If the list is sorted in ascending order, stop as soon as you pass the place where the target would be: an item greater than the target means it can't be later in the list.
def linear_search_sorted(items, target):
for i in range(len(items)):
if items[i] == target:
return i # found
if items[i] > target:
return -1 # gone past it: not in the list
return -1 # reached the end
Comparison for the sorted list [3, 7, 10, 15, 20]:
| Target | Unsorted-style search | Sorted early exit |
|---|---|---|
| 10 | found at index 2 (3 comparisons) | found at index 2 (3 comparisons) |
| 12 | 5 comparisons, then -1 | 4 comparisons: stops at 15 (> 12), returns -1 |
| 1 | 5 comparisons, then -1 | 1 comparison: stops at 3 (> 1), returns -1 |
Precondition: the list must be sorted. If it isn't, the early exit can wrongly report a missing item.
Counting comparisons
def search_count(items, target):
comparisons = 0
for i in range(len(items)):
comparisons += 1
if items[i] == target:
print(comparisons, "comparisons")
return i
print(comparisons, "comparisons")
return -1
print(search_count([8, 15, 4, 23, 16, 42], 23))
# 4 comparisons
# 3
print(search_count([8, 15, 4, 23, 16, 42], 99))
# 6 comparisons
# -1
Summary of the versions
| Version | Stops early? | Needs sorted data? | Comparisons |
|---|---|---|---|
| Worst case (no exit) | No | No | Always |
| Early exit when found | When found | No | 1 to |
| Sorted, early exit when found | When found | Sorted | 1 to |
| Sorted, early exit when not in list | When found or passed | Sorted | Fewer for missing items |
Worked example
Task: a sorted list of student IDs [101, 105, 110, 120, 135] is searched for 112. Write the code, and say how many comparisons are made.
ids = [101, 105, 110, 120, 135]
def find_id(items, target):
for i in range(len(items)):
if items[i] == target:
return i
if items[i] > target:
return -1
return -1
print(find_id(ids, 112)) # -1
Trace: compare 101 (not equal, not greater), 105, 110, then 120 is greater than 112: stop and return -1. That's 4 comparisons, instead of 5 for a full scan.
Exam tip: choose the version that matches the data: early exit when found for any list, and early exit when not found only for a sorted list. return -1 (or None) for "not found" and make sure it comes after the loop, not inside it. Count comparisons carefully when asked.
Check yourself
How many comparisons does the worst-case version make for a list of 20 items?
Always 20, because it checks every item.
How does a sorted list allow an early exit when the target isn't present?
When an item greater than the target is reached, the target can't appear later, so the search stops and reports not found.
Why must return -1 for "not found" be placed after the loop?
If it is inside the loop it would run on the first item that isn't equal, ending the search too early.
What is the best case for a linear search with early exit?
The target is the first item, so only one comparison is made.