💻 Computing

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.

IntermediateUpdated 5 Nov 2026
GCSEA-LevelHigh School

Curriculum 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].

  1. The middle item is 17, and 26 is larger, so keep [21, 26, 31].
  2. 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].

  1. Compare 5 and 2: swap, giving [2, 5, 4, 1].
  2. Compare 5 and 4: swap, giving [2, 4, 5, 1].
  3. 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?

  1. Binary search needs a sorted list.
  2. 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

1. Binary search requires the list to be…
2. Bubble sort works by…
3. Linear search checks…
4. In [2, 5, 9, 14, 20], binary search for 14 first checks…
5. An algorithm is…

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.

Take this guide with you

Get a free printable PDF with the worked examples and quiz answers. Enter your email and the download starts straight away.

Sources and review

Reviewed by StudyMorrow editorial review on 5 Nov 2026.