- How a linear search checks items one at a time.
- How a binary search repeatedly halves a sorted list.
- Why binary search is usually faster, but only works in certain conditions.
- How to compare the advantages and disadvantages of both algorithms.
A searching algorithm is a set of steps used to find whether a particular item is present in a collection of data.
In GCSE questions, the data is often shown as a list or array. An array is a data structure that stores multiple values under one name. Each value is an element. Each element has a position called an index.
Many programming examples use index 0 for the first item:
The item you are looking for is often called the target value or search item.
Searching algorithm
A searching algorithm is a step-by-step method for finding a target value in a list, or deciding that the target value is not present.
Follow the indexing in the question
Some exam questions label positions starting at 0, while others may describe the “first item”, “second item”, and so on. Use the indexes or positions given in the question rather than assuming.
A linear search checks each element in order, starting at the beginning of the list.
It compares the current element with the target value:
- If they match, the search stops because the item has been found.
- If they do not match, the algorithm moves to the next element.
- If the end of the list is reached without a match, the target is not in the list.
Linear search in one sentence
Linear search works by checking every item one by one until it finds the target or reaches the end of the list.
The diagram shows a linear search for 56. Notice that it does not jump around — it checks the list from left to right.

Here is one clear version of linear search in AQA-style pseudo-code:
found ← False
index ← 0
WHILE found = False AND index < LENGTH(values)
IF values[index] = target THEN
found ← True
ELSE
index ← index + 1
ENDIF
ENDWHILE
IF found = True THEN
OUTPUT index
ELSE
OUTPUT "Not found"
ENDIF
The variable found is a Boolean variable, meaning it can only store True or False.
Tracing a linear search
Search for 28 in this list:
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
|---|
| Value | 15 | 4 | 28 | 9 | 28 | 11 |
- Compare the first item with the target: index 0 stores 15, and 15 is not 28, so move on.
- Compare the next item: index 1 stores 4, and 4 is not 28, so move on again.
- Compare index 2: it stores 28, which matches the target, so the search stops and returns index 2.
A basic linear search usually stops as soon as it finds the first match. In the example above, 28 also appears at index 4, but the algorithm returns index 2 because that is the first match found.
Duplicate values
If a list contains duplicates, a simple search may find a matching value, not necessarily every matching value. To find all matches, the algorithm would need to keep searching after the first match.
Linear search is simple and flexible. It works even if the list is not sorted.
However, it can be slow for long lists because it may have to check many items. If the target is the last item, or not present at all, linear search checks every element.
A binary search is a more efficient search method, but it has one very important requirement: the list must already be sorted.
A list is sorted when its values are in a clear order, such as smallest to largest or alphabetical order.
Binary search works by repeatedly checking the middle item of the current search area.
The basic idea is:
- Look at the middle item.
- If it is the target, stop.
- If the target is smaller than the middle item, ignore the right half.
- If the target is larger than the middle item, ignore the left half.
- Repeat with the remaining half.
Using binary search on an unsorted list
Binary search only works correctly on a sorted list. If the data is not sorted, discarding half the list might throw away the target value by mistake.
The diagram shows binary search finding 57 in a sorted list. Each comparison removes a large part of the remaining search area.

Binary search often uses three index variables:
-
left — the first index still being searched
-
right — the last index still being searched
-
middle — the index halfway between left and right
found ← False
left ← 0
right ← LENGTH(values) - 1
WHILE found = False AND left <= right
middle ← (left + right) DIV 2
IF values[middle] = target THEN
found ← True
ELSEIF values[middle] < target THEN
left ← middle + 1
ELSE
right ← middle - 1
ENDIF
ENDWHILE
IF found = True THEN
OUTPUT middle
ELSE
OUTPUT "Not found"
ENDIF
DIV means integer division. It gives the whole-number result and ignores any remainder. For example, 7 DIV 2 gives 3.
Tracing a binary search
Search for 57 in this sorted list:
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|
| Value | 3 | 8 | 12 | 19 | 25 | 31 | 44 | 57 | 68 |
- Start with
left ← 0 and right ← 8. Calculate middle ← (0 + 8) DIV 2, so middle ← 4. The value at index 4 is 25.
- Compare 25 with the target 57. Since 57 is larger, ignore index 4 and everything to its left. Set
left ← 5.
- Now
left ← 5 and right ← 8. Calculate middle ← (5 + 8) DIV 2, so middle ← 6. The value at index 6 is 44.
- Compare 44 with 57. Since 57 is larger, ignore index 6 and everything to its left. Set
left ← 7.
- Now
left ← 7 and right ← 8. Calculate middle ← (7 + 8) DIV 2, so middle ← 7. The value at index 7 is 57, which matches the target, so the search stops and returns index 7.
Binary search stops when the remaining search area becomes empty. In the pseudo-code, this happens when left > right.
For example, if left becomes 6 and right is 5, there are no indexes left to check. The algorithm can safely output "Not found".
Both algorithms are used to find data, but they are suitable in different situations.
| Feature | Linear search | Binary search |
|---|
| Does the list need to be sorted? | No | Yes |
| How does it search? | Checks items one by one | Checks the middle and halves the search area |
| Works on unsorted data? | Yes | No |
| Simple to understand and program? | Very simple | Slightly more complex |
| Good for small lists? | Yes | Yes |
| Good for large sorted lists? | Less suitable | More suitable |
Choosing between them
Use linear search when the data is unsorted or the list is small. Use binary search when the data is sorted and you want to reduce the number of comparisons.
Choosing a search algorithm
A program needs to search a list of 500 student surnames. The surnames are stored in alphabetical order. Decide which search algorithm is more suitable.
- Check whether the data is sorted. The surnames are in alphabetical order, so binary search is allowed.
- Consider the size of the list. There are 500 surnames, so checking one by one could take many comparisons.
- Choose binary search because it can repeatedly halve the sorted list, making it more suitable than linear search here.
Saying binary search is always best
Binary search is not automatically the best choice. If the list is unsorted, binary search cannot be used unless the data is sorted first.
In an exam, you may be asked to “explain how” an algorithm works. That means you should describe the mechanics, not just name the algorithm.
For linear search, include ideas like:
- starts at the first item
- compares each item with the target
- moves through the list in order
- stops when found or when the end is reached
For binary search, include ideas like:
- the list must be sorted
- compares the target with the middle item
- discards the half where the target cannot be
- repeats until found or until no items remain
In the exam
- For a trace question, keep track of the current index or the
left, right and middle values after each comparison.
- For binary search, always state that the list must be sorted before the algorithm can work correctly.
- When comparing algorithms, give both a benefit and a limitation, such as “linear works on unsorted data but may check every item”.
Check yourself
- Why can binary search discard half of the list after each comparison?
- What happens in a linear search if the target value is not present?
- Which search algorithm would you choose for a short unsorted list, and why?