An algorithm is a sequence of steps that can be followed to complete a task. This topic is about how algorithms are written down, how they are built out of smaller parts, and how you judge whether one is better than another. It is the foundation of Paper 1: almost every programming question assumes you can do what is set out here. Learn it as a set of tools for thinking — decomposition, abstraction, tracing and comparing — rather than as a list of definitions.
What you need to know
What a search does
A search algorithm looks through a set of data for a particular item. The question every search question turns on is how many comparisons it makes, because that is the work the computer does. Two searches are on the specification: linear search and binary search, and you have to be able to explain the mechanics of both.
Key fact: A search finds an item in a set of data, and its cost is measured in comparisons.
How linear search works
Linear search starts at the first item and checks each item in turn until it finds a match or runs out of data. If the item is at position 1 it takes one comparison; if it is not there at all it takes as many comparisons as there are items. It works on data in any order, which is its great advantage.
Key fact: Linear search checks each item in turn from the start, and works on unsorted data.
How binary search works
Binary search needs the data to be sorted. It looks at the middle item and compares it with the target: if they match, it is found; if the target is smaller, it discards the upper half of the remaining data; if larger, it discards the lower half. It repeats on what is left. Each comparison therefore halves the number of possible places.
Key fact: Binary search halves the remaining data each step, so it needs sorted data.
How many comparisons each takes
This is the number to memorise: a linear search on n items takes at best 1 comparison and at worst n comparisons. A binary search on n items takes about log2(n) comparisons, so 8 comparisons on 100 items, and 10 comparisons on 1000 items. On a list of 1000 items, linear search takes up to 1000 comparisons and binary search about 10.
Key fact: Linear search: up to n comparisons. Binary search: about log2(n) — roughly 10 on 1000 items.
The advantages and disadvantages
Binary search is dramatically faster on large data sets, but it needs the data to be sorted, and sorting it costs time. If the data changes constantly, sorting before every search may cost more than it saves. Linear search needs no preparation at all and is fine on small data sets. So the choice depends on the data, not on which algorithm is 'better'.
Key fact: Binary search is faster on large data but needs sorting; linear search needs none.
Choosing one in an exam answer
When you are asked which to use, the answer needs the condition attached: 'binary search, because the list is sorted and contains many items', not just the name. If the data is unsorted, or small, or changes constantly, linear search is the right answer — and saying why is what earns the mark.
Key fact: Always give the condition with the choice: sorted and large means binary.
Flashcards
How many comparisons does a linear search make in the worst case on n items?
n, because every item has to be checked.
What must the data be for a binary search?
Sorted.
How many comparisons does a binary search make on 1000 items?
About 10, because the number of comparisons is about log2 of the number of items.
What is the main advantage of binary search?
It is much faster on large data sets because it halves the remaining data each step.
What is the main advantage of linear search?
It needs no preparation — the data does not have to be sorted.
What happens if the target item is the first item in a linear search?
It is found on the first comparison.
What does a binary search do when the target is larger than the middle item?
It discards the lower half of the remaining data and continues on the upper half.
Exam questions
Written in the style of AQA Computer Science, with a mark scheme for each one. No question here is a real past paper question.
mcq1 mark
A list of 1000 items is sorted and you need to find one item. Which search would you use, and why?
Linear search, because it needs no preparation
Binary search, because it needs about 10 comparisons instead of up to 1000
Linear search, because it always finds the item first
Binary search, because it does not need the data to be sorted
Mark scheme: 1 mark for choosing binary search because it makes about log2(1000) = 10 comparisons instead of up to 1000 for a linear search. The data is sorted, which is the condition binary search requires.
short3 marks
Explain in English the mechanics of a binary search, and state what must be true of the data first.
Mark scheme: 1 mark for stating that the data must be sorted. 1 mark for explaining that the search looks at the middle item and compares it with the target. 1 mark for explaining that the comparison discards half of the remaining data each time, and that the process repeats until the item is found or the data runs out.
six6 marks
Explain in English the differences between linear and binary search, and when you would choose each.
Mark scheme: Level 1 (1-2 marks): A short answer saying linear checks every item and binary halves the list. Level 2 (3-4 marks): A structured answer covering the mechanics of each and the number of comparisons. Level 3 (5-6 marks): A detailed answer explaining that a linear search checks items in turn from the start, costing at best 1 and at worst n comparisons, and that a binary search needs sorted data and halves the remaining data each step, costing about log2(n) comparisons — about 10 on 1000 items — and explaining that the choice depends on the data: sorted, large and stable data favours binary search, while unsorted, small or frequently changing data favours linear search, so a full answer names the algorithm and gives the condition.