DoAssignment study guide

D4.2 · Investigate a theoretical computer-science topic

Learn to investigate a theoretical computer-science topic through clear examples and targeted practice.

Ontario Grade 12 Computer Science

Topics in Computer Science

A focused theoretical investigation for Ontario Grade 12 Computer Science

A theoretical computer-science investigation studies a focused question about a computing idea. It identifies assumptions, examines evidence, and draws a conclusion that the evidence supports. This lesson investigates how binary search finds a target in a sorted list and what a comparison count tells us. We will trace particular inputs rather than treating one example as proof about every possible search.

What you will learn

  • Describe a theoretical computer-science investigation as a focused question supported by evidence and a reasoned conclusion.
  • Explain the precondition that makes binary search valid.
  • Trace binary search and count comparisons for a particular input.
  • Distinguish what one example shows from what can be concluded more generally.

1. Start with a question and assumptions

From ICS3U, you should be familiar with lists, indexes, selection, and repetition. Those ideas are enough for this investigation. The examples use distinct numbers and zero-based indexes, so the first item is at index 0.
Our question is: when a list is sorted, how does binary search use comparisons to narrow the possible location of a target? We can answer by explaining the algorithm, tracing chosen inputs, and counting the element-to-target checks. A comparison count here means one check of a list value against the target.
A trace is evidence about the particular input traced. It can establish exactly which values this search inspected, but that one result does not settle what happens for all possible lists and targets. State the conditions and scope of a conclusion clearly.
  • Define a focused question before examining an algorithm.
  • Make input assumptions explicit.
  • Use a trace as evidence about its stated example, not as a universal result.

2. The model: inspect the middle and narrow the range

Binary search requires a list sorted in ascending order. It also assumes that the values being examined can be accessed by index. Keep track of the lowest and highest indexes that could still contain the target. Inspect the midpoint of that range and compare its value with the target.
If the values match, the search is finished. If the target is smaller, continue in the lower-index portion; if it is larger, continue in the higher-index portion. Sorted order makes these choices valid: values on the discarded side cannot contain the target. When the lowest remaining index is greater than the highest, there are no candidate positions left, so report that the target is absent.
The expression below chooses an integer index in the current range. Integer division discards any fractional part, which is needed because list indexes are whole numbers.
The Python 3 code is for reading, not a claim that it has been executed. Preconditions: the list is sorted in ascending order and contains distinct values. Input: a list and a target. The algorithm checks a midpoint and narrows the range until it finds a match or the range is empty. Expected output: the matching index, or -1 if the target is absent.
mid=⌊low+high2⌋mid=\left\lfloor\frac{low+high}{2}\right\rfloor
  • Sorted order justifies discarding one side after a comparison.
  • The midpoint must be an integer index within the current range.
  • An empty candidate range means the target was not found.

3. Trace and count carefully

A useful trace records the current range, the midpoint, the value inspected, and the decision. These details make it possible to check whether the next range follows from the comparison. Count one comparison each time the algorithm checks a list value against the target.
An absent target can require more than one check. A mismatch at a midpoint does not usually prove absence; it rules out only one portion. Keep narrowing until a match is found or there are no indexes left.
A comparison with linear search is meaningful when both searches use the same list and target. Linear search checks in order from the start. Its count for an absent target is the number of list values checked. These are counts for the examples, not claims about all inputs.
  • Count each inspected value once.
  • Show enough trace detail to justify each range change.
  • Compare algorithms on a specified, shared input.

4. Draw a conclusion within the evidence

Suppose a trace shows binary search finding a target after three comparisons and a left-to-right linear search finding it after six checks. That supports a statement about those exact searches. It does not establish that binary search always takes fewer comparisons for every list and target.
The sorted-input requirement is central, not a minor setup preference. Binary search uses the order of values to rule out part of the list. On unsorted input, a value in a discarded portion might still be the target, so the decision is not justified. A single successful search on an unsorted list would not make that method reliable.
When investigating another computer-science topic, use the same discipline: ask a focused question, state assumptions, gather and check evidence, and make a conclusion no broader than the evidence allows.
  • Separate a calculated result from a general claim.
  • Explain the assumption that makes an algorithm step valid.
  • A conclusion should answer the question and state the limits of the evidence.

Worked example

Trace a successful search

Use binary search on [4, 9, 15, 22, 31, 46, 58] to find 22. State the midpoint index, comparison count, and returned index.
Indexes and values
Index → stored value0419215322431546658

The first midpoint is index 3, which contains the target.

Python 3

def binary_search(values, target):
    low = 0
    high = len(values) - 1
    while low <= high:
        mid = (low + high) // 2
        if values[mid] == target:
            return mid
        if target < values[mid]:
            high = mid - 1
        else:
            low = mid + 1
    return -1
  1. Set the initial range
    The list has indexes 0 through 6, so all seven positions are candidates. The midpoint index is 3.
    low=0,high=6,mid=3low=0,\quad high=6,\quad mid=3
  2. Check the midpoint
    The value at index 3 is 22, equal to the target. The search stops after this single comparison and returns the index.
    A[3]=22A[3]=22
Answer: The midpoint index is 3. Binary search makes 1 comparison and returns index 3.
Check: Only one list value, the value at index 3, was compared with the target.

Worked example

Trace an absent target and compare counts

For the sorted list [3, 8, 12, 19, 27, 35, 41], trace binary search for 20. Then count the checks a left-to-right linear search makes before reporting that 20 is absent.
Comparison counts for this input
Binary search3Linear search7

Calculated counts for the stated list and absent target 20; these are example results, not general benchmarks.

Python 3

def linear_search(values, target):
    for index in range(len(values)):
        if values[index] == target:
            return index
    return -1
  1. Inspect the first midpoint
    The initial range is indexes 0 through 6. Its midpoint is index 3, containing 19. Since the target is larger, only indexes 4 through 6 remain candidates.
    mid=3,A[3]=19mid=3,\quad A[3]=19
  2. Narrow the range again
    The midpoint of indexes 4 through 6 is index 5, containing 35. Since the target is smaller, only index 4 remains a candidate.
    mid=5,A[5]=35mid=5,\quad A[5]=35
  3. Finish binary search
    Index 4 contains 27. The target is smaller, so the remaining range becomes empty. Binary search reports absence after three comparisons.
    mid=4,A[4]=27mid=4,\quad A[4]=27
  4. Count linear-search checks
    Because 20 is absent, linear search must check all seven values before it can report absence. The binary-search count of three and linear-search count of seven describe only this list and target.
    3<73<7
Answer: Binary search inspects indexes 3, 5, and 4, then reports absence after 3 comparisons. Linear search checks all 7 elements before reporting absence.
Check: The binary trace has three midpoint checks. Linear search checks every element because none equals 20.

Worked example

Investigate why sorted input matters

A student applies binary search to [2, 14, 6, 20, 11] to find 11. Explain why the method is not justified and what must change before using binary search appropriately.
Original and sorted values
Index → stored valueOriginal list2, 14, 6, 20, 11Sorted list2, 6, 11, 14, 20

The second row shows the same values arranged in ascending order.

Python 3

def binary_search(values, target):
    low = 0
    high = len(values) - 1
    while low <= high:
        mid = (low + high) // 2
        if values[mid] == target:
            return mid
        if target < values[mid]:
            high = mid - 1
        else:
            low = mid + 1
    return -1
  1. Check the precondition
    The list is not ascending: 14 is followed by 6. Binary search relies on sorted order to justify discarding a portion, so its conclusion on this input is not reliable.
  2. Examine the first decision
    The midpoint of indexes 0 through 4 is index 2, containing 6. Since 11 is larger, the method would keep indexes 3 through 4. Here 11 happens to be at index 4, so this decision keeps it. That outcome does not justify binary search on unsorted lists: another arrangement could place the target in the discarded portion.
    mid=2,A[2]=6mid=2,\quad A[2]=6
  3. Make the method appropriate
    Sort the values into ascending order before using binary search. Then its ordering-based decisions have the required condition. Alternatively, use a search method that does not rely on sorted order.
    [2,6,11,14,20][2,6,11,14,20]
Answer: Binary search is not justified on the supplied unsorted list. Although the first decision happens to keep the target, that does not establish reliability. Sort the values into ascending order before applying binary search.
Check: The issue is whether the algorithm's assumption is satisfied, not whether this particular unsorted arrangement happens to preserve the target in its candidate range.

Common mistakes and how to avoid them

Using binary search on an unsorted list because a midpoint can still be calculated.
Correction: The midpoint calculation is possible, but discarding a portion is justified only when the list is sorted.
Treating one trace's comparison count as the count binary search always needs.
Correction: Report the count for the exact input and target. A single example does not describe every possible search.
Confusing the target value with its index.
Correction: The target is the value sought; the returned index is its position. In the first example, 22 is at index 3.
Reporting absence after the first unequal midpoint.
Correction: An unequal comparison usually rules out only one portion. Continue until a match is found or no candidate indexes remain.

Lesson summary

  • A theoretical investigation uses a focused question, explicit assumptions, evidence, and a conclusion supported by that evidence.
  • Binary search checks a midpoint and keeps only the portion that could contain the target.
  • Ascending sorted order is essential to the reasoning that makes discarded portions impossible candidates.
  • Count comparisons from the actual trace; do not turn one example count into an unsupported general claim.

Check your understanding

Question 1

A sorted list has indexes 0 through 6. What is the first midpoint index?
  1. 2
  2. 3
  3. 4
  4. 6
Show answer and explanation
3
The midpoint is the whole-number result of dividing the endpoint sum by 2: (0 + 6) // 2 = 3.

Question 2

Why can binary search discard the higher-index portion when the target is smaller than the midpoint value?
  1. Ascending order means values in that portion cannot be smaller than the midpoint value.
  2. Higher indexes are always empty.
  3. Binary search checks every element before choosing a portion.
  4. A target smaller than a midpoint is always absent.
Show answer and explanation
Ascending order means values in that portion cannot be smaller than the midpoint value.
In ascending order, values at higher indexes are at least as large as the midpoint value, so they cannot contain a smaller target.

Question 3

Binary search checks midpoint values 18, 26, and 21 while looking for 20. How many comparisons have been observed so far?
  1. 2
  2. 3
  3. 20
  4. The count cannot be known until the target is found.
Show answer and explanation
3
Each inspected midpoint value is one comparison. Three values have been inspected, so the observed count is 3.

Key terms

Theoretical computer-science investigation
A focused study of a computer-science idea using clear assumptions, examples or other evidence, and a supported conclusion.
Precondition
A condition that must be true before an algorithm is used. Binary search requires sorted input.
Midpoint
The middle index of the current range of candidate positions.
Comparison count
The number of element-to-target checks made in a particular search trace.

Continue through ICS4U

View the complete ICS4U Ontario Grade 12 Computer Science curriculum and lessons

Tutor support for D4.2 · Investigate a theoretical computer-science topic

Working on D4.2 · Investigate a theoretical computer-science topic 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.

برای مبحث D4.2 · بررسی موضوع نظری علوم رایانه در درس ICS4U، سؤال و تلاش خود را همراه بیاورید. در جلسه می‌توانید یک مثال کوچک را دنبال کنید و نخستین گامی را که خروجی با انتظار شما فرق دارد پیدا کنید. راهنمای معلم برنامه‌نویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقه‌ای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی می‌شود.

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation D4.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.

Official curriculum reference

Report a correction or ask a question