How do linear and binary search work, and which is faster?
Understand the linear search and binary search algorithms, how each works, their requirements, and their time complexity.
A focused answer to AQA A-Level Computer Science 4.3.2, covering the linear search and binary search algorithms, how each works, the requirement that binary search needs sorted data, and their time complexity.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
What this dot point is asking
AQA wants you to describe how linear search and binary search work, state that binary search requires a sorted list, give the time complexity of each, and trace either algorithm on supplied data.
Linear search
FOR i = 0 TO length - 1
IF list[i] = target THEN RETURN i
ENDFOR
RETURN -1 # not found
Linear search has time complexity : in the worst case (item last or absent) it makes comparisons. Its best case is when the target is the first item, and the average case is roughly comparisons, which is still . Its strength is that it needs no preparation of the data.
Binary search
WHILE low <= high
mid = (low + high) DIV 2
IF list[mid] = target THEN RETURN mid
ELSE IF list[mid] < target THEN low = mid + 1
ELSE high = mid - 1
ENDWHILE
RETURN -1 # not found
The bounds low and high define the part of the list still being searched; updating them to mid + 1 or mid - 1 (never mid itself) guarantees the search space shrinks every loop, so the algorithm always terminates.
Comparing them
Binary search is dramatically faster for large lists: searching a million items takes up to about 20 comparisons rather than a million, because . The trade-off is that the list must be sorted first, which has its own cost (an sort); for a small list, an unsorted list, or a list searched only once, linear search may be the simpler and faster overall choice.
A useful way to decide is to think about how often you search versus how often the data changes. If a list is searched many thousands of times and rarely changes, paying the one-off cost of sorting it so that every subsequent search is is clearly worthwhile. If the data is constantly changing or each search happens only once, the sort cost is never recovered, so a plain linear scan wins. This is why a database keeps sorted indexes (built once, searched constantly) while a one-off scan through a log file uses linear search.
Both algorithms can be written either iteratively (using a loop) or recursively (the function calls itself on the smaller sub-problem). Binary search lends itself naturally to recursion because each step solves a smaller version of the same problem on half the list, and the recursion depth of about is exactly why its time complexity is logarithmic. Examiners frequently ask you to trace either version step by step, so practise writing out the index examined and the bounds at every iteration rather than just stating the final answer.
Exam-style practice questions
Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AQA 20184 marksA binary search is performed on the sorted list [3, 8, 12, 19, 25, 31, 47, 52] to find the value 31. Trace the algorithm, stating the index examined at each step and the value found there, until the target is located. Indices start at 0.Show worked answer →
Set low , high .
Step 1: mid , list[3] . , so search the upper half: low .
Step 2: mid , list[5] . Match found at index 5.
Markers reward correctly computing each midpoint using integer division, comparing with the target, narrowing the bounds the right way, and reaching index 5 in two steps. Showing the bound updates is what earns the method marks.
AQA 20213 marksExplain why binary search has a time complexity of O(log n) while linear search has a time complexity of O(n), and state the requirement binary search places on the data.Show worked answer →
Linear search checks items one at a time from the start, so in the worst case (target last or absent) it makes comparisons, giving : the work grows in direct proportion to the list size.
Binary search halves the remaining search space at each comparison, so after comparisons at most items remain. The number of steps to reduce to 1 is about , hence . This requires the data to be sorted, because the algorithm relies on knowing that everything below the midpoint is smaller and everything above is larger.
Markers reward the halving argument for , the linear pass for , and stating the sorted-data requirement.
Related dot points
- Understand the bubble sort and merge sort algorithms, how each orders a list, their time complexity, and the trade-off between them.
A focused answer to AQA A-Level Computer Science 4.3.3, covering the bubble sort and merge sort algorithms, how each works, their time complexity, and the trade-off between simplicity and efficiency.
- Understand Big-O notation for time and space complexity, the common orders of growth, how to determine the complexity of an algorithm, and the meaning of best, average and worst case.
A focused answer to AQA A-Level Computer Science 4.3.5, covering Big-O notation for time and space complexity, the common orders of growth, determining the complexity of an algorithm, and best, average and worst case.
- Understand trees as a connected, undirected graph with no cycles, the terms root, child, parent, leaf and subtree, and the structure and use of a binary search tree.
A focused answer to AQA A-Level Computer Science 4.2.5, covering trees as connected acyclic graphs, the terminology root, child, parent and leaf, binary trees, and the structure and use of a binary search tree.
- Understand Dijkstra's shortest path algorithm, how it finds the lowest-cost path from a start vertex, the role of a priority queue, and its applications.
A focused answer to AQA A-Level Computer Science 4.3.4, covering Dijkstra's shortest path algorithm, how it finds the lowest-cost path from a start vertex in a weighted graph, the role of a priority queue, and its applications.
- Understand arrays (one, two and three dimensional), records and fields, and the difference between static and dynamic data structures.
A focused answer to AQA A-Level Computer Science 4.2.1, covering one, two and three dimensional arrays, records and fields, indexing, and the difference between static and dynamic data structures.
Sources & how we know this
- AQA A-level Computer Science (7517) specification — AQA (2015)