DoAssignment study guide
C2.2 · Compare linear and binary search costs
Learn to compare linear and binary search costs through clear examples and targeted practice.
Ontario Grade 12 Computer Science
Designing Modular Programs
Trace the comparisons, check the input conditions, and make a fair comparison
What you will learn
- Describe how linear search and binary search examine a list.
- Trace both methods and count value-to-target comparisons.
- Compare search costs while recognizing that binary search requires sorted input.
Prerequisite review: lists and comparisons
- Python list indexes begin at 0.
- Count checks of list values against the target.
- Binary search requires a sorted list.
How the methods search
- Linear search checks in order and can stop at a match.
- Binary search repeatedly narrows a sorted search interval.
- A trace count depends on the target and its position, or on whether it is absent.
Python 3 methods: input, steps, and expected results
- Both methods take a list and a target and return an index or -1.
- Binary search’s narrowing decisions are valid only for sorted input.
- Trace the stated code by following its comparisons; do not assume a result without checking.
Comparing costs fairly
- Name the input condition and target when reporting costs.
- A specific trace count is not a universal count for that list length.
- Binary search is not guaranteed to use fewer comparisons for every target.
Worked example
A target found after several checks
For the stated nine-value sorted list: index labels identify the method; values are comparison counts.
Python 3
def linear_search(values, target):
for index, value in enumerate(values):
if value == target:
return index
return -1
def binary_search(values, target):
low, high = 0, len(values) - 1
while low <= high:
middle = (low + high) // 2
if values[middle] == target:
return middle
if values[middle] < target:
low = middle + 1
else:
high = middle - 1
return -1- Trace linear searchIndexes 0 through 5 are checked in order. The value at index 5 is 31, so the search stops there.
- Trace binary searchThe first middle index is 4, containing 25. Since 31 is larger, continue at indexes 5 through 8. The next middle index is 6, containing 42, so continue at index 5. That value is 31, and the search returns index 5.
Worked example
An absent target
For the stated twelve-value sorted list: index labels identify the method; values are comparison counts.
Python 3
values = [3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47]
target = 22
# Preconditions: values is sorted for binary search.
# Expected result from each search: -1- Trace linear searchThe target is absent, so linear search checks each of the twelve values before returning -1.
- Trace binary searchCheck index 5, value 23; continue lower. Check index 2, value 11; continue higher. Check index 3, value 15; continue higher. Check index 4, value 19; continue higher. No positions remain, so return -1.
Worked example
A match at the first position
For the stated eight-value sorted list: index labels identify the method; values are comparison counts.
Python 3
values = [2, 6, 10, 14, 18, 22, 26, 30]
target = 2
# Preconditions: values is sorted for binary search.
# Expected result from each search: index 0- Trace linear searchThe value at index 0 is 2, so linear search finds the target and stops after its first comparison.
- Trace binary searchCheck index 3, value 14, and continue in indexes 0 through 2. Check index 1, value 6, and continue at index 0. Check 2 at index 0 and return that index.
Common mistakes and how to avoid them
Lesson summary
- Linear search checks values in order and can work with an unsorted list.
- Binary search checks middle values and narrows the interval, but requires sorted input.
- Count comparisons from the exact trace and distinguish that count from a general upper bound.
Check your understanding
Question 1
- 1
- 3
- 7
- It depends on whether the list is sorted.
Show answer and explanation
Question 2
- The list has an even number of values.
- The list is sorted.
- The target is at the middle index.
- The list has no repeated values.
Show answer and explanation
Question 3
- 1
- 2
- 3
- 5
Show answer and explanation
Key terms
- Comparison
- A check of one list value against the target value.
- Linear search
- A method that checks list values in sequence until it finds the target or reaches the end.
- Binary search
- A method for sorted input that checks a middle value and continues in the part that could still contain the target.
- Precondition
- A condition that must be true before using a method; binary search requires sorted input.
Continue through ICS4U
View the complete ICS4U Ontario Grade 12 Computer Science curriculum and lessons
Tutor support for C2.2 · Compare linear and binary search costs
Working on C2.2 · Compare linear and binary search costs in ICS4U? Bring the question and your own attempt. In a live session, you can trace a small example and find the first step where the output differs from what you expected. Ask about the first step that does not make sense, then try a similar question yourself.
Explore coding tutoring · Request an eligible free 30-minute introduction. Your request is matched by subject and language; a tutor must accept before a time is confirmed.
برای مبحث C2.2 · مقایسه هزینه جستوجوی خطی و دودویی در درس ICS4U، سؤال و تلاش خود را همراه بیاورید. در جلسه میتوانید یک مثال کوچک را دنبال کنید و نخستین گامی را که خروجی با انتظار شما فرق دارد پیدا کنید. راهنمای معلم برنامهنویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقهای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی میشود.
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation C2.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.