Skip to main content
SingaporeComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.87 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 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 nn items it may need up to nn 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:

  1. Look at the middle item.
  2. If it is the target, stop.
  3. If the target is smaller, repeat on the left half.
  4. 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 10001000 items needs at most about 1010 comparisons by binary search (210>10002^{10} > 1000), but up to 10001000 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 1616 items. State the maximum number of comparisons a binary search needs. [2 marks]

  • Cue. 44, because 24=162^4 = 16, 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 [3,7,11,15,19,23,27][3, 7, 11, 15, 19, 23, 27]. Trace a binary search for the value 1919. 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 1515, 2323, 1919, so the value was found in 33 comparisons.

Markers reward checking the middle each time, moving right then left correctly, finding 1919, and the count of 33 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 10001000 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 10001000 comparisons (every item). Binary search needs at most about 1010, because each step halves the list (210=1024>10002^{10} = 1024 > 1000).

Markers reward checking items in turn for linear search, binary search needing sorted data to halve safely, and roughly 10001000 versus 1010 comparisons.

Related dot points