Skip to content

Cheat sheet

GCSE algorithm cheat sheet

Linear search, binary search, bubble sort, insertion sort and merge sort on one page, with pseudocode, worked traces and demos you can step through.

At a glance

AlgorithmWhat it doesNeeds a sorted list?Speed on a long list
Linear searchChecks every item in orderNoSlow
Binary searchHalves the list each stepYesFast
Bubble sortSwaps neighbouring pairs–Slow
Insertion sortInserts each item into a sorted part–Slow, but good on nearly sorted lists
Merge sortSplits, then merges in order–Fast

You do not need Big O notation at GCSE. Compare algorithms in words: how many comparisons they make and whether the list must be sorted.

  1. Start at the first item.
  2. If it matches the target, stop: found.
  3. Otherwise move to the next item.
  4. If you reach the end, the target is not in the list.
found ← False
i ← 0
WHILE i < LEN(items) AND found = False
  IF items[i] = target THEN
    found ← True
  ENDIF
  i ← i + 1
ENDWHILE

Binary search only works on a sorted list. It checks the middle item, then throws away the half that cannot contain the target.

low ← 0
high ← LEN(items) - 1
found ← False
WHILE low ≤ high AND found = False
  mid ← (low + high) DIV 2
  IF items[mid] = target THEN
    found ← True
  ELSE IF items[mid] < target THEN
    low ← mid + 1
  ELSE
    high ← mid - 1
  ENDIF
ENDWHILE
Exam favourite: “Explain why binary search is more efficient than linear search.” Say that each comparison halves the number of items left to check, so far fewer comparisons are needed on a large list, but the list must be sorted first.

Bubble sort

  1. Compare the first two items. If they are in the wrong order, swap them.
  2. Move along one place and repeat to the end of the list. That is one pass.
  3. After each pass the largest unsorted item is in its final place.
  4. Repeat passes until a full pass makes no swaps.

Worked trace: [5, 3, 8, 1]

PassList after the passSwaps
1[3, 5, 1, 8]2
2[3, 1, 5, 8]1
3[1, 3, 5, 8]1
4[1, 3, 5, 8]0, so stop

Insertion sort

  1. Treat the first item as a sorted list of one.
  2. Take the next item and move it left past every larger item.
  3. Insert it in the gap. The sorted part is now one item longer.
  4. Repeat until every item has been inserted.

Trace on [5, 3, 8, 1]: [3, 5, 8, 1] → [3, 5, 8, 1] → [1, 3, 5, 8].

Merge sort

  1. Divide: split the list in half again and again until every list has one item.
  2. Merge: merge pairs of lists, comparing the front items and taking the smaller each time.
  3. Keep merging until one sorted list remains.

Trace on [6, 2, 7, 3]: [6, 2] [7, 3] → [6] [2] [7] [3] → [2, 6] [3, 7] → [2, 3, 6, 7].

Exam checklist

  • Can you trace each algorithm on a list of four to eight items?
  • Can you say which algorithm a piece of pseudocode is?
  • Can you explain why binary search needs a sorted list?
  • Can you give one advantage and one disadvantage of each sort?

Questions

Common questions

Which algorithms are on my exam board?

OCR J277 includes linear search, binary search, bubble sort, merge sort and insertion sort. AQA 8525 includes linear search, binary search, bubble sort and merge sort. Always check your own specification.

Do I need to write these in code?

You may be asked to complete or write a search or sort in pseudocode or your programming language, so practise writing linear and binary search in particular.

Is there a printable version?

Use your browser's print option to save this page. For personalised practice, book a GCSE lesson with Dee.

Want help with this one to one?

Dee replies personally to every enquiry, usually within two working days.

Send an enquiry