How do linear and binary search work, and why is binary search so much faster on a sorted list?
Searching algorithms: linear search, binary search and binary tree search, how each works with a trace, the precondition for binary search, and their time complexity in Big-O notation.
An OCR H446 answer on searching algorithms: how linear search, binary search and binary tree search work with worked traces, the requirement that binary search needs a sorted list, and the time complexity of each in Big-O notation.
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
OCR wants you to describe and trace linear search, binary search and binary tree search, state the precondition for binary search, and give each algorithm's time complexity in Big-O. This is trace-table and complexity territory: expect to complete a trace and to compare complexities.
The answer
Linear search
function linearSearch(list, target)
for i = 0 to length(list) - 1
if list[i] == target then
return i
endif
next i
return -1 // not found
endfunction
Binary search
Binary tree search
Examples in context
A dictionary or phone book lookup is naturally a binary search, you open in the middle and discard half. Databases index data in balanced trees (B-trees) so lookups are logarithmic. Linear search is used where data is small, unsorted or searched once (sorting first would not pay off). The choice illustrates the trade-off OCR stresses: sorting costs time but enables fast repeated binary searches. This links directly to sorting algorithms and to Big-O analysis.
Try this
Q1. State the precondition that must hold before binary search can be used. [1 mark]
- Cue. The list must be sorted.
Q2. State the worst-case time complexity of linear search and of binary search. [2 marks]
- Cue. Linear search ; binary search .
Q3. Explain why binary tree search can degrade to . [2 marks]
- Cue. If the tree is unbalanced (for example built from sorted data into a long chain), its height approaches , so a search may have to descend nodes.
Exam-style practice questions
Practice questions written in the style of OCR exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
OCR 20196 marksA sorted list contains (indices 0 to 6). Using a trace table, show how a binary search finds the value 19, and state the time complexity of binary search.Show worked answer →
Trace (up to 5):
| low | high | mid | list[mid] | comparison |
|---|---|---|---|---|
| 0 | 6 | 3 | 15 | , so search right; low = 4 |
| 4 | 6 | 5 | 23 | , so search left; high = 4 |
| 4 | 4 | 4 | 19 | found at index 4 |
Each step computes , compares, and halves the range. The value 19 is found at index 4 after three comparisons.
Complexity (1 mark): binary search is because the search space halves each step. Markers reward a correct trace table with mid recalculated and the range narrowing, and the complexity.
OCR 20216 marksExplain why linear search has time complexity while binary search has , and explain the condition that must hold before binary search can be used.Show worked answer →
Linear search (up to 2): linear search checks each item in turn from the start; in the worst case (item last or absent) it examines all items, so the number of comparisons grows in proportion to .
Binary search (up to 2): binary search compares the target with the middle item and discards half the remaining items each step; the number of steps needed to reduce items to one is , so the work grows logarithmically.
Condition (up to 2): binary search requires the list to be sorted, because it relies on knowing that everything to one side of the middle is smaller (or larger) than the target. On an unsorted list it cannot decide which half to discard. Markers reward "checks all in the worst case" for linear, "halves each step" for binary, and the sorted precondition.
Related dot points
- Sorting algorithms: bubble sort, insertion sort, merge sort and quick sort, how each works with a trace, and their time complexity in Big-O notation including best, average and worst cases.
An OCR H446 answer on sorting algorithms: how bubble sort, insertion sort, merge sort and quick sort order a list with worked traces, and their time complexity in Big-O notation including the difference between the quadratic and the divide-and-conquer sorts.
- Graph and tree traversal: breadth-first and depth-first traversal of a graph, the pre-order, in-order and post-order traversal of a binary tree, and the shortest-path algorithms Dijkstra's algorithm and A* search.
An OCR H446 answer on traversal and shortest-path algorithms: breadth-first and depth-first graph traversal, the pre-order, in-order and post-order traversal of a binary tree, and how Dijkstra's algorithm and A* search find a shortest path.
- Big-O notation for time and space complexity: the constant, logarithmic, linear, linearithmic, polynomial and exponential complexity classes, how to determine the Big-O of an algorithm, and using it to compare and judge the suitability of algorithms.
An OCR H446 answer on Big-O notation: the constant, logarithmic, linear, linearithmic, polynomial and exponential complexity classes for time and space, how to determine an algorithm's Big-O, and using it to compare algorithms and judge their suitability.
- Data structures: arrays, records, tuples and lists, the stack and queue abstract data types and their operations, linked lists, trees and graphs, and hash tables, including how each is used and its advantages.
An OCR H446 answer on data structures: arrays, records, tuples and lists, the stack and queue abstract data types with their operations, linked lists, trees, graphs and hash tables, including how each is used and its advantages and disadvantages.
- Thinking logically: identifying the decision points and conditions that affect the flow of a solution; computational methods including problem recognition, divide and conquer, backtracking, heuristics, performance modelling and visualisation.
An OCR H446 answer on thinking logically and computational methods: identifying decision points and conditions in a solution, and the methods of problem recognition, divide and conquer, backtracking, heuristics, performance modelling and visualisation that make problems solvable.