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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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 , but if the data is sorted, binary search discards half the possibilities each step and runs in .
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 comparisons, so it is . Best case is 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 comparisons: .
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 ) 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 clearly beats .
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 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. - when the target is the last element or is absent, so all 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 comparisons, since .
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 (indices 0 to 7). Trace a binary search for the value , 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 .
| 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 is found at index 5 after 2 comparisons.
Markers reward the correct mid calculation each step, narrowing to the right half after , 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 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 in the worst case (the item is last or absent, so all are checked). Binary search is (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 , the worst case is about comparisons, since .
Markers reward the two complexities, the sorted precondition with the reason it matters, and roughly 20 comparisons from of a million.
Related dot points
- Analyse the time and space complexity of an algorithm using Big-O notation, and compare common growth rates
A focused answer to the H2 Computing outcome on algorithmic complexity. Big-O notation, deriving the order of growth from loops and recursion, the common complexity classes, and the trade-off between time and space.
- Implement and trace bubble sort and insertion sort, analyse their complexity, and compare their behaviour on nearly sorted data
A focused answer to the H2 Computing outcome on elementary sorts. Bubble sort and insertion sort, tracing each on an example, their O(n squared) worst case, and why insertion sort excels on nearly sorted input.
- Explain and trace merge sort and quicksort as divide-and-conquer algorithms, analyse their complexity, and compare their trade-offs
A focused answer to the H2 Computing outcome on efficient sorts. Merge sort and quicksort as divide-and-conquer, their O(n log n) behaviour, quicksort's worst case, and the trade-off between stability, memory and speed.
- Define recursion in terms of base and recursive cases, trace recursive calls, and compare recursion with iteration
A focused answer to the H2 Computing outcome on recursion. Base and recursive cases, tracing the call stack, the divide-and-conquer pattern, and the trade-offs between recursion and iteration.
- Describe a hash table and hash function, explain collision resolution by chaining and open addressing, and analyse the effect of load factor
A focused answer to the H2 Computing outcome on hash tables. Hash functions mapping keys to buckets, average O(1) lookup, collisions resolved by chaining or open addressing, and how the load factor affects performance.