Binary search and bubble sort: searching and sorting algorithms
Learn how linear search, binary search and bubble sort work, with step-by-step traces and when to use each.
GCSEA-LevelHigh SchoolCurriculum fit: KS3 and GCSE Computing / Computer Science (algorithms: searching and sorting); A-Level. US: no US standards source is linked here: check your school's computer science curriculum.
On this page
What is it?
An algorithm is a step-by-step set of instructions for solving a problem. Algorithms can be shown as flowcharts or pseudocode. Two common tasks are searching for an item in a list and sorting a list into order.
Different algorithms suit different situations, and computer scientists compare them by how many steps they take as the list grows.
Key formulas and ideas
- Linear search: check each item in turn until you find the target. It works on any list.
- Binary search: needs a sorted list. Compare with the middle item and discard half the list each time.
- Bubble sort: repeatedly compare neighbouring items and swap them if they are in the wrong order.
- Merge sort: split the list, sort the halves and merge them (faster for large lists).
Worked examples
Example 1: Binary search
Question: Find 26 in [3, 8, 12, 17, 21, 26, 31].
- The middle item is 17, and 26 is larger, so keep [21, 26, 31].
- The middle item is 26, which is the target.
Answer: Found in 2 comparisons
Example 2: One pass of bubble sort
Question: Show one pass of bubble sort on [5, 2, 4, 1].
- Compare 5 and 2: swap, giving [2, 5, 4, 1].
- Compare 5 and 4: swap, giving [2, 4, 5, 1].
- Compare 5 and 1: swap, giving [2, 4, 1, 5].
Answer: [2, 4, 1, 5]
Example 3: Choose an algorithm
Question: Which search suits an unsorted list?
- Binary search needs a sorted list.
- Linear search works on any list.
Answer: Linear search
Common mistakes
- Using binary search on an unsorted list.
- Stopping bubble sort after one pass.
- Forgetting to compare the final item.
- Mixing up the middle item calculation with an off-by-one error.
Practice questions
Frequently asked questions
Why not always use binary search?
Because it only works on sorted data. If the list is not sorted, you must sort it first or use linear search.
Why not always use binary search?
It only works on sorted data. If the list is not sorted you must sort it first or use linear search.
Related guides
Programming basics: variables, if statements and loops
Understand variables, data types, selection and loops, with pseudocode examples you can trace by hand.
KS3GCSEMiddle SchoolHigh SchoolBinary numbers: convert binary to decimal and back
Learn how computers store data as bits and bytes, convert between binary and decimal and add binary numbers, with worked examples.
KS3GCSEMiddle SchoolHigh SchoolHow the internet works: IP addresses, DNS and packets
Learn what happens when you open a website, from DNS lookups to packets and HTTPS, in plain English.
KS3GCSEMiddle SchoolHigh SchoolSources and review
Reviewed by StudyMorrow editorial review on 5 Nov 2026.