How do linear and binary search find an item, and why is binary search faster?
Describe and trace linear search and binary search, and explain why binary search needs a sorted list
A focused answer to the O-Level Computing point on searching. How linear search checks each item in turn, how binary search halves a sorted list each step, and why binary search is faster but 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 describe and trace two search algorithms, linear search and binary search, and to explain why binary search needs a sorted list and is faster. The central idea is that linear search checks items one by one, while binary search repeatedly halves a sorted list, so it reaches the answer in far fewer steps.
The answer
Linear search
A linear search starts at the first item and checks each one in turn until it finds the target or runs out of items.
FOR each item in the list
IF item = target THEN
OUTPUT "found"
STOP
ENDIF
NEXT
OUTPUT "not found"
It works on any list, sorted or not. In the worst case (the item is last or missing), it checks every item, so for items it may need up to comparisons.
Binary search
A binary search works only on a sorted list. It checks the middle item, then discards the half that cannot contain the target:
- Look at the middle item.
- If it is the target, stop.
- If the target is smaller, repeat on the left half.
- If the target is larger, repeat on the right half.
Each step throws away half the remaining items, so the list shrinks quickly.
list: [3, 7, 11, 15, 19, 23, 27] target 11
middle 15 -> 11 < 15, take left half [3, 7, 11]
middle 7 -> 11 > 7, take right half [11]
middle 11 -> found
Why binary search needs a sorted list
Binary search depends on knowing that everything left of the middle is smaller and everything right is larger. That is only true if the list is sorted. On an unsorted list this assumption fails, so the algorithm could throw away the half that actually holds the target.
Why binary search is faster
Because each step halves the list, the number of comparisons grows very slowly. A list of items needs at most about comparisons by binary search (), but up to by linear search. The bigger the list, the greater the advantage.
Examples in context
Example 1. Finding a name in a phone book. A printed phone book is sorted, so you naturally use a binary-search style: open the middle, decide which half the name is in, and repeat. You never read every entry, which is why a sorted book is quick to search.
Example 2. Searching an unsorted inbox. Looking through unsorted emails for one from a particular sender, with no order to exploit, is a linear search: you check messages one by one until you find it. Sorting the inbox by sender first would let a binary-search approach work.
Try this
Q1. State one advantage of linear search over binary search. [2 marks]
- Cue. It works on any list, sorted or not, with no need to sort the data first.
Q2. A sorted list has items. State the maximum number of comparisons a binary search needs. [2 marks]
- Cue. , because , so the list halves at most four times.
Q3. Explain why binary search needs the list to be sorted. [2 marks]
- Cue. It discards a half based on order; only a sorted list guarantees the target is not in the discarded half.
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.
Original5 marksA sorted list holds . Trace a binary search for the value . State the middle value checked at each step and whether the search goes left or right, then give the number of comparisons.Show worked answer →
Binary search checks the middle, then halves the list.
Step 1: list [3,7,11,15,19,23,27], middle = 15. 19 > 15, search the right half.
Step 2: right half [19,23,27], middle = 23. 19 < 23, search the left half.
Step 3: left half [19], middle = 19. Found.
The middle values checked were , , , so the value was found in comparisons.
Markers reward checking the middle each time, moving right then left correctly, finding , and the count of comparisons.
Original5 marks(a) Describe how a linear search works. (b) Explain why binary search cannot be used on an unsorted list. (c) For a list of items, state roughly the maximum number of comparisons each method needs.Show worked answer →
(a) A linear search checks each item in turn from the start until it finds the target or reaches the end of the list. If it reaches the end without a match, the item is not present.
(b) Binary search works by comparing the target with the middle item and discarding the half that cannot contain it. This only works if the list is sorted, because the algorithm relies on knowing that everything to one side of the middle is smaller and everything to the other side is larger. On an unsorted list that assumption is false, so it could discard the half that actually contains the item.
(c) Linear search may need up to comparisons (every item). Binary search needs at most about , because each step halves the list ().
Markers reward checking items in turn for linear search, binary search needing sorted data to halve safely, and roughly versus comparisons.
Related dot points
- Define an algorithm and represent its sequence, selection and iteration using standard flowchart symbols
A focused answer to the O-Level Computing point on algorithms and flowcharts. What an algorithm is, the standard flowchart symbols, and how to show sequence, selection and iteration in a flowchart.
- Write algorithms in pseudocode using input, output, assignment, selection (IF) and iteration (WHILE, FOR)
A focused answer to the O-Level Computing point on pseudocode. Writing algorithms with input, output, assignment, IF selection, and WHILE and FOR loops, in clear language-independent steps.
- Describe and trace the bubble sort, explaining how repeated passes of comparisons and swaps sort a list
A focused answer to the O-Level Computing point on sorting. How bubble sort compares neighbouring items and swaps them, how each pass moves the largest value to the end, and tracing the algorithm by hand.
- Use a trace table to follow variable values through an algorithm, and choose normal, boundary and invalid test data
A focused answer to the O-Level Computing point on tracing and testing. Building a trace table to track variables through an algorithm, and choosing normal, boundary and invalid test data to find errors.
- Use Python lists and strings, including indexing, length, looping over items, and basic operations like append and slicing
A focused answer to the O-Level Computing point on Python lists and strings. Creating lists, zero-based indexing, finding length with len(), looping over items, appending, and basic string operations and slicing.