DoAssignment study guide
A3.2 · Linear and Binary Search: Trace and Compare
Learn to write linear and binary search through clear examples and targeted practice.
Ontario Grade 12 Computer Science
Programming Concepts and Skills
ICS4U A3.2: finding a value in a list
What you will learn
- Describe the input conditions and steps for linear search.
- Write a linear search that reports whether a target is present and where it is found.
- Explain why binary search needs a sorted list and how each comparison narrows the search.
- Trace linear and binary search and report their comparison counts for a given example.
1. Prerequisite review: lists, indexes, and results
- Python list indexes begin at 0.
- Choose a clear not-found result, such as -1.
- Binary search requires values in ascending order.
2. Linear search: check items in order
- Start at index 0 and move forward one position after each mismatch.
- Return immediately when the target matches.
- If the loop finishes, the target was not found.
3. Binary search: narrow a sorted list
- Set the initial search range to the full list.
- Compare with the middle item and discard the middle plus the impossible side.
- Stop with -1 when the search range becomes empty.
4. Compare methods and check the conditions
- Linear search has no sorted-input requirement.
- Binary search is correct only when the list is sorted as required.
- Trace actual comparisons rather than guessing a count.
Worked example
Linear search finds a target
Each value is checked from left to right; the target is found at index 3.
Python 3
def linear_search(items, target):
for index in range(len(items)):
if items[index] == target:
return index
return -1- Set up the searchThe input list is [14, 6, 21, 9, 17] and the target is 9. Linear search needs no sorted-order precondition. Start at index 0 and compare each value in turn.
- Trace the comparisonsAt index 0, 14 is not 9. At index 1, 6 is not 9. At index 2, 21 is not 9. At index 3, 9 matches, so stop rather than checking index 4.
- Report the resultThe target is at index 3. Four list values were compared with the target, so this trace uses four comparisons.
Worked example
Linear search reports an absent target
Every item is checked; none matches the target 7.
Python 3
def linear_search(items, target):
for index in range(len(items)):
if items[index] == target:
return index
return -1- Set up the searchThe input list is [5, 12, 8, 20] and the target is 7. No sorted order is needed. Since the target may be absent, keep checking until either a match is found or the list ends.
- Check every itemIndex 0 contains 5, index 1 contains 12, index 2 contains 8, and index 3 contains 20. None equals 7. All four items have therefore been compared.
- Return the not-found valueThe loop has no more list positions to check. Returning -1 communicates that the target was not found; it is not an index in this list.
Worked example
Binary search in a sorted list
The sorted list is narrowed from indexes 0–6 to 4–6, then 4–4.
Python 3
def binary_search(items, target):
left = 0
right = len(items) - 1
while left <= right:
middle = (left + right) // 2
if items[middle] == target:
return middle
if target < items[middle]:
right = middle - 1
else:
left = middle + 1
return -1- Check the preconditionThe list is in ascending order, so binary search is appropriate. Set left to index 0 and right to index 6. The target is 24.
- Compare at the middleThe middle index is 3, whose value is 19. Since 24 is greater than 19, the target, if present, must be to the right. Set left to index 4.
- Search the remaining rangeThe range is now indexes 4 through 6. Its middle index is 5, whose value is 31. Since 24 is less than 31, set right to index 4.
- Find the targetThe remaining range contains only index 4. Its value is 24, so return index 4. The trace made three comparisons: with 19, 31, and 24.
Common mistakes and how to avoid them
Lesson summary
- Linear search checks list items from the beginning and works whether or not the list is sorted.
- Binary search compares at the middle of a sorted list and continues only in the side that could contain the target.
- Both methods should return a matching index or a clearly defined not-found result.
- A trace should show the checked values and, for binary search, the changing search boundaries.
Check your understanding
Question 1
- Binary search, because it always checks the middle.
- Linear search, because it can check every item without relying on order.
- Either method, because order does not affect the comparisons.
- correctIndex":1,"explanation":"Linear search checks items in sequence and does not depend on sorted order. Binary search needs sorted input."}
Show answer and explanation
Question 2
- Indexes 0 through 2
- Indexes 3 through 4
- Only index 2
- correctIndex":1,"explanation":"The list is ascending and 18 is greater than 11, so only values to the right of index 2 can contain it: indexes 3 and 4."}
Show answer and explanation
Question 3
- Index 2; 3 comparisons
- Index 3; 4 comparisons
- -1; 4 comparisons
- correctIndex":0,"explanation":"The values at indexes 0, 1, and 2 are compared in order. The third comparison finds 13 at index 2."}
Show answer and explanation
Key terms
- Target
- The value a search is trying to find.
- Index
- The numbered position of an item in a list; Python indexes start at 0.
- Precondition
- A condition that must be true for an algorithm to be used correctly; binary search requires sorted input.
- Comparison
- A check of whether a list value equals, is less than, or is greater than the target.
Continue through ICS4U
View the complete ICS4U Ontario Grade 12 Computer Science curriculum and lessons
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation A3.2. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.