Searching and sorting algorithms: linear, binary, bubble, insertion and merge
Two ways to search and three ways to sort. Know the steps of each, what each needs, and apply it to a short list by hand.
By the end you can
- Describe the main steps of linear search and binary search
- Explain why binary search needs a sorted list
- Apply bubble sort, insertion sort and merge sort to a short list by hand
- Compare the algorithms and choose one for a given situation
Key words
- Algorithm
- A precise set of steps that solves a problem.
- Linear search
- Checks each item in turn, from the start, until it finds the target or runs out of items.
- Binary search
- Checks the middle item of a sorted list, then throws away the half that cannot hold the target.
- Bubble sort
- Compares neighbouring pairs and swaps any in the wrong order, pass after pass.
Searching finds an item in a list, and sorting puts a list in order. Both come up on every computer science course, and the exam expects you to run them by hand.
Linear search checks every item in turn
Linear search starts at the first item and compares it with the target. If they match, the search stops. If not, it moves to the next item, and so on until it finds the target or reaches the end.
Its big advantage is that it works on any list, sorted or not. Its drawback is speed. If the target is last, or missing, every item gets checked, so a list of 1,000 items can need 1,000 checks.
Binary search halves the list each time
Binary search has one pre-requisite: the list must already be sorted. It checks the middle item first. If that is the target, it stops. If the target is smaller, it throws away the middle item and everything after it. If the target is bigger, it throws away the middle item and everything before it.
It then repeats on the half that is left. Because each check halves what remains, a sorted list of 1,000 items needs at most 10 checks. When there are two middle items, pick a rule, such as the left one, and use it every time.
Here is binary search looking for 42 in the sorted list 3, 8, 15, 23, 31, 42, 57
- The middle item is 23, the fourth of seven. 42 is bigger, so 23 and everything before it are thrown away.
- That leaves 31, 42 and 57. The middle item is 42, which matches, so the search stops after two checks.
Following along on your device? Tap a step’s number to tick it off.
A linear search for 42 in the same list would need six checks, one for each item up to and including 42.
Bubble sort swaps neighbours
Bubble sort compares the first two items and swaps them if they are in the wrong order. It then compares the second and third, and carries on to the end. That is one pass, and after it the biggest item has bubbled to the end of the list.
It keeps making passes until a whole pass makes no swaps, which means the list is sorted. Sorting 5, 2, 8, 1 into ascending order goes like this:
Swipe the table sideways to see every column →
| After | List |
|---|---|
| Start | 5, 2, 8, 1 |
| Pass 1 | 2, 5, 1, 8 |
| Pass 2 | 2, 1, 5, 8 |
| Pass 3 | 1, 2, 5, 8 |
| Pass 4 | 1, 2, 5, 8 (no swaps, so stop) |
Bubble sort is simple to understand and to code. On a long list, though, it makes a great many comparisons, so it is slow.
Insertion sort builds a sorted part
Insertion sort treats the first item as a sorted list of one. It takes the next item and slides it left past every bigger item until it reaches its place. Then it takes the next item, and repeats until none are left.
Sorting 4, 3, 1, 2, the sorted part grows from the left. After placing 3 the list is 3, 4, 1, 2. After placing 1 it is 1, 3, 4, 2, and after placing 2 it is 1, 2, 3, 4. Insertion sort is quick on short lists and on lists that are nearly sorted already.
Merge sort splits, then merges
Merge sort splits the list in half, and keeps splitting until every item is on its own. A list of one item is already sorted. It then merges pairs of lists, always taking the smaller front item next, until one sorted list is left.
Sorting 6, 3, 7, 1 splits into 6, 3, 7 and 1 on their own. Merging pairs gives 3, 6 and 1, 7. Merging those two gives 1, 3, 6, 7. Merge sort is much faster than bubble sort on long lists. The cost is extra memory, because it builds new lists as it merges.
Choosing an algorithm
Exam questions often ask which algorithm suits a situation. This table sums up the trade-offs:
Swipe the table sideways to see every column →
| Algorithm | Needs a sorted list? | Good for | Drawback |
|---|---|---|---|
| Linear search | No | Short or unsorted lists | Slow on long lists |
| Binary search | Yes | Long sorted lists | The list must be sorted first |
| Bubble sort | Not a search | Learning, and very short lists | Very slow on long lists |
| Insertion sort | Not a search | Short or nearly sorted lists | Slow on long, jumbled lists |
| Merge sort | Not a search | Long lists | Uses extra memory |
You do not need to memorise code for these algorithms. You do need to recognise each one from its code or pseudocode, and apply it to a list. The sorting tool and the searching tool let you step through each one.
Check your understanding
Have a go first, then open each answer to check.
1.What must be true about a list before you can use binary search on it?
Show the answer
It must be sorted. Binary search throws away half the list after each check, which only works if the items are in order.
2.Show the list 7, 4, 9, 2 after the first pass of a bubble sort into ascending order.
Show the answer
4, 7, 2, 9. 7 and 4 swap, 7 and 9 stay, then 9 and 2 swap.
3.A list has 6, 2, 5 and 1. Show it after insertion sort has placed the 2 and the 5.
Show the answer
After placing 2 it is 2, 6, 5, 1. After placing 5 it is 2, 5, 6, 1.
4.Merge the sorted lists 2, 9 and 4, 5 into one sorted list.
Show the answer
2, 4, 5, 9. Compare the front items each time and take the smaller: 2, then 4, then 5, then 9.
5.Give one advantage of linear search over binary search.
Show the answer
Linear search works on an unsorted list, so there is no need to sort the data first.
6.Why is merge sort usually chosen over bubble sort for a very long list?
Show the answer
Merge sort makes far fewer comparisons on a long list, so it finishes much sooner. Bubble sort compares neighbouring pairs on every pass.
Key words
- Algorithm
- A precise set of steps that solves a problem.
- Linear search
- Checks each item in turn, from the start, until it finds the target or runs out of items.
- Binary search
- Checks the middle item of a sorted list, then throws away the half that cannot hold the target.
- Bubble sort
- Compares neighbouring pairs and swaps any in the wrong order, pass after pass.
- Insertion sort
- Takes each item in turn and slides it into its place in the sorted part of the list.
- Merge sort
- Splits the list into single items, then merges sorted lists back together in order.
- Pass
- One trip through the list in bubble sort.
- Pre-requisite
- Something that must be true before an algorithm can work, such as a sorted list.
Sources
The curriculum statements and specification points on this page come from these documents, read on 2 December 2026. Specifications are updated from time to time, so check your exam board's current version.
- GCSE (9-1) Computer Science J277 specification, Version 3.1 (May 2026) (OCR)
- GCSE Computer Science 8525 specification, For first teaching in September 2025 (AQA)
Spotted something wrong or out of date? Tell us and we will correct it.
More for GCSE and Key stage 4
- Knowledge organiserData and binaryBinary, denary and hexadecimal: converting, adding and shiftingEvery number from 0 to 255 fits in 8 bits. Learn the place values and you can convert to denary or hex, add, and shift.4 min read · Years 10 and 11
- Knowledge organiserComputer systemsLogic gates and truth tables: AND, OR and NOTAND needs both inputs on, OR needs at least one, and NOT flips its input. Combine them and a truth table shows every outcome.3 min read · Years 10 and 11