Linear and Binary Search
Construct and trace both search algorithms, then choose a method based on ordering, indexing and efficiency requirements.
What you need to be able to do
- construct and trace a linear search;
- construct and trace a binary search on appropriately ordered data;
- compare the efficiency of linear and binary search;
- select a search technique based on the data organization and search key.
Rapid Recall Deck
Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.
Trace one item at a time
names = ["Ava", "Ben", "Cara", "Drew", "Eli"]
target = "Drew"
index = 0
found = -1
while index < len(names) and found == -1:
if names[index] == target:
found = index
else:
index += 1
print(found)Trace the search
State the value checked on each iteration and the final output.
The algorithm checks Ava at index 0, Ben at 1, Cara at 2, then Drew at 3. It sets found = 3 and the loop stops. Final output: 3.
Write linear search without a built-in search
in, .index() or any search library.Construct linear_search(data, target)
Return the index of the first matching value, or -1 when the target is not present.
def linear_search(data, target):
for i in range(len(data)):
if data[i] == target:
return i
return -1Discard half of an ordered search range
["Ava", "Ben", "Cara", "Drew", "Eli", "Faye", "Gus"], target "Eli".Trace low, high and mid
low=0, high=6, mid=3→ Drew. Eli is later alphabetically, solow=4.low=4, high=6, mid=5→ Faye. Eli is earlier, sohigh=4.low=4, high=4, mid=4→ Eli. Found at index 4.
Make the range shrink correctly
Construct binary_search(data, target)
Assume data is already sorted in ascending order. Return the matching index or -1.
def binary_search(data, target):
low = 0
high = len(data) - 1
while low <= high:
mid = (low + high) // 2
if data[mid] == target:
return mid
elif data[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1The search key matters
Find a phone number from a name
The data is ordered by the key being searched. Binary search can eliminate half the remaining names after each comparison.
Find a name from a phone number
The records are not ordered by phone number. Without a separate phone-number index, binary search is not valid for this lookup.
Explain the algorithm choice
Why is binary search suitable for the first lookup but linear search may be required for the second?
The directory is sorted by name, so comparing a target name with the middle record tells the algorithm which half can be discarded; binary search therefore gives O(log n) search growth. A phone-number lookup does not have that ordering information, so unless a separate index or ordering by phone number exists, the safe method is to inspect records one by one using linear search, which is O(n).
Teacher-created Paper 2 practice
1. A sorted List contains [4, 9, 13, 18, 25, 31, 42]. Trace binary search for target 25 by recording low, high and mid until the target is found.
2. Explain why binary search cannot be safely applied to [18, 4, 31, 9, 25] without first changing how the data is organized.
3. Construct a linear search function that returns True when a target exists and False otherwise. Do not use in.
low=0, high=6, mid=3→ 18, so low=4. Thenlow=4, high=6, mid=5→ 31, so high=4. Thenlow=4, high=4, mid=4→ 25.- The data is not sorted by the values being compared, so a middle comparison does not tell which half can be safely discarded.
def contains(data, target): for value in data: if value == target: return True return False
Finish the learning cycle
IA — main task
Look at any search or lookup behaviour planned for the IA. Record what key is searched and whether the data is naturally ordered or indexed by that key.
Syllabus — short retrieval
Redo the binary-search trace on page 9 without Reveal, then explain in one sentence why sorted data is a precondition rather than just a performance improvement.