Skip to main content
SingaporeComputer ScienceSyllabus dot point

How do we find an item in a collection, and why is a sorted list so much faster to search?

Implement and trace linear search and binary search, state their complexities, and justify when each applies

A focused answer to the H2 Computing outcome on searching. Linear search and binary search, tracing each on an example, their O(n) and O(log n) complexities, and the precondition that binary search needs sorted data.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

SEAB wants you to implement and trace linear search and binary search, state their time complexities, and justify when each is appropriate. The central idea is that exploiting structure pays off: a linear search makes no assumptions and is O(n)O(n), but if the data is sorted, binary search discards half the possibilities each step and runs in O(logn)O(\log n).

The answer

Linear search

Linear search checks each element in turn until it finds the target or runs out:

def linear_search(data, target):
    for index in range(len(data)):
        if data[index] == target:
            return index
    return -1   # not found

It works on any list, sorted or not. In the worst case (target last or absent) it makes nn comparisons, so it is O(n)O(n). Best case is O(1)O(1) if the target is first.

Binary search

Binary search needs a sorted list. It compares the target with the middle element and discards the half that cannot contain it:

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 target > data[mid]:
            low = mid + 1
        else:
            high = mid - 1
    return -1   # not found

Each step halves the range, so it needs at most log2n\log_2 n comparisons: O(logn)O(\log n).

Why the precondition matters

Binary search decides which half to keep based on order. On unsorted data that decision is invalid, so it can skip past the target and report "not found" incorrectly. The list must be sorted first; if it is not, either sort it (costing O(nlogn)O(n \log n)) or use linear search.

Choosing between them

  • Use linear search for small or unsorted lists, or when the list changes constantly so keeping it sorted is not worth it.
  • Use binary search when the list is sorted (or stays sorted) and is large enough that O(logn)O(\log n) clearly beats O(n)O(n).

Examples in context

Example 1. A phone contact list. Looking up a contact in an alphabetically sorted list is binary search: open the middle, decide earlier or later, repeat. Finding one name among ten thousand takes about 14 comparisons, which is why sorted indexes feel instant.

Example 2. Searching a log file as it is written. A live log appended to constantly is not kept sorted by content, so finding an error message means a linear scan. Here O(n)O(n) is accepted because maintaining sort order on every append would cost more than the occasional search saves.

Try this

Q1. State the worst-case complexity of linear search and explain when it occurs. [2 marks]

  • Cue. O(n)O(n) - when the target is the last element or is absent, so all nn items are compared.

Q2. Why must a list be sorted for binary search to work? [2 marks]

  • Cue. Binary search discards a half based on whether the target is greater or less than the middle value; without order, that decision is invalid and the target can be missed.

Q3. Roughly how many comparisons does binary search need for a sorted list of 1024 items in the worst case? [1 mark]

  • Cue. About log21024=10\log_2 1024 = 10 comparisons, since 210=10242^{10} = 1024.

Exam-style practice questions

Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Original6 marksA sorted list is [3,8,12,19,27,33,41,55][3, 8, 12, 19, 27, 33, 41, 55] (indices 0 to 7). Trace a binary search for the value 3333, showing the low, high and middle indices at each step and the comparison made. State how many comparisons were needed.
Show worked answer →

Binary search repeatedly checks the middle of the current range. The middle index is lfloor(low+high)/2rfloor\\lfloor (low + high) / 2 \\rfloor.

Step low high mid value at mid comparison
1 0 7 3 19 33 > 19, search right
2 4 7 5 33 33 == 33, found

The target 3333 is found at index 5 after 2 comparisons.

Markers reward the correct mid calculation each step, narrowing to the right half after 33>1933 > 19, and finding the value at index 5 in two comparisons.

Original4 marks(a) State the worst-case time complexity of linear search and of binary search on a list of nn items. (b) Explain the precondition binary search requires and why it cannot be ignored. (c) For a list of one million items, roughly how many comparisons does binary search need in the worst case?
Show worked answer →

(a) Linear search is O(n)O(n) in the worst case (the item is last or absent, so all nn are checked). Binary search is O(logn)O(\\log n) (the range halves each step).

(b) Binary search requires the list to be sorted. It decides which half to discard based on whether the target is greater or less than the middle value; if the data is unsorted, that decision is meaningless and the algorithm can miss the item entirely.

(c) For n=1,000,000n = 1\\,000\\,000, the worst case is about log2(106)approx20\\log_2(10^6) \\approx 20 comparisons, since 220approx1062^{20} \\approx 10^6.

Markers reward the two complexities, the sorted precondition with the reason it matters, and roughly 20 comparisons from log2\\log_2 of a million.

Related dot points