Skip to content

Searching and sorting algorithms: GCSE Computer Science revision notes

Check each item in turn, starting at the beginning, until you find the target or reach the end of the list. It works on unsorted lists. It is simple, but slow for large lists: a list of n items may need n comparisons.

In short

Check each item in turn, starting at the beginning, until you find the target or reach the end of the list. It works on unsorted lists. It is simple, but slow for large lists: a list of n items may need n comparisons.

For GCSE students in Years 10 and 11, on the AQA, OCR, Edexcel, Eduqas, Edexcel IGCSE and Cambridge IGCSE Computer Science courses.

Linear search

  • Check each item in turn, starting at the beginning, until you find the target or reach the end of the list.
  • It works on unsorted lists. It is simple, but slow for large lists: a list of n items may need n comparisons.

Binary search

  • It only works on a sorted list. Look at the middle item. If it is the target, stop. If the target is smaller, discard the upper half; if it is larger, discard the lower half. Repeat on the half that is left.
  • It is much faster for large lists: 1000 items need at most 10 comparisons, because each comparison halves the list.
  • When there are two middle items, pick one of them consistently (for example the lower one).

Bubble sort

  • Go through the list comparing each pair of neighbouring items, and swap them if they are in the wrong order. Each pass moves the largest remaining item to its correct place at the end.
  • Keep making passes until a whole pass makes no swaps.
  • It is simple to code, but slow for large lists.

Merge sort and insertion sort

  • Merge sort: split the list in half again and again until every list has one item, then merge the lists back together in order. It is much faster than bubble sort for large lists, but uses more memory.

Key terms

Linear search
Checking each item in turn until the target is found.
Binary search
Repeatedly checking the middle item of a sorted list and discarding half.
Bubble sort
Repeatedly comparing and swapping neighbouring items until no swaps are needed.
Merge sort
Splitting a list into single items, then merging them back together in order.
Insertion sort
Inserting each item into its correct place in a growing sorted part.
Pass
One complete trip through the list in a sort.

Quick questions on Searching and sorting algorithms

Which search only works on a sorted list?
Binary search: It relies on knowing which half the target is in.
How does a linear search work?
It checks each item in turn from the start: It stops when it finds the target or reaches the end.
In a binary search, the target is smaller than the middle item. What happens next?
The upper half is discarded: The target must be in the lower half.
What is the maximum number of comparisons a binary search needs for 1000 sorted items?
10: 2¹⁰ = 1024, so 10 halvings are enough.
What does a bubble sort compare?
Pairs of neighbouring items: It swaps a pair if they are in the wrong order.
After one pass of a bubble sort into ascending order, where is the largest item?
At the end of the list: It 'bubbles' to the end.
When does a bubble sort stop?
When a pass makes no swaps: No swaps means the list is in order.
What is the first stage of a merge sort?
Split the list in half repeatedly until each list has one item: Then the single items are merged back together in order.
What is one disadvantage of a merge sort compared with a bubble sort?
It uses more memory: It needs extra space for the split lists.
What is one advantage of a linear search?
It works on an unsorted list: Binary search needs the list to be sorted first.
A bubble sort starts on the list 5, 2, 4. What is the list after the first swap?
2, 5, 4: 5 and 2 are compared and swapped first.
How does an insertion sort work?
It takes each item and inserts it into the correct place in the sorted part: The sorted part at the start grows by one item each time.
What does this linear search print?
Found at 2: It checks 4 (index 0), 8 (index 1), then 15 at index 2, and stops.
This is one pass of a bubble sort. What does it print?
[1, 4, 2, 5]: Swap 5 and 1, then 5 and 4, then 5 and 2. After one pass the biggest number has bubbled to the end, but the list is not sorted yet.
A binary search starts with the middle item. Which item does it check first?
12: low is 0 and high is 6, so mid is 6 // 2 = 3. items[3] is 12.
How many comparisons does this linear search make?
3: It compares 7, then 3, then 9, which matches, so it stops after 3 comparisons.

Searching and sorting algorithms: exam-style questions and mark schemes

  1. Which algorithm needs the data to be sorted first? [1 mark]

    Answer: Binary search

  2. Describe how a binary search would find 11 in the list 1, 3, 5, 7, 9, 11, 13. [3 marks]

    • The middle item, 7, is compared with 11 (1)
    • 11 is larger, so the lower half is discarded, leaving 9, 11, 13 (1)
    • The new middle item is 11, so the target is found (1)
  3. Show how a bubble sort would put the list 4, 1, 3, 2 into ascending order. Give the list after each pass. [4 marks]

    • After pass 1: 1, 3, 2, 4 (1)
    • After pass 2: 1, 2, 3, 4 (1)
    • Pass 3 makes no swaps (1)
    • So the sort stops, with the list 1, 2, 3, 4 (1)
  4. Give one advantage and one disadvantage of a merge sort compared with a bubble sort. [2 marks]

    • Advantage: much faster and more efficient for large lists (1)
    • Disadvantage: it uses more memory, or is more complex to code (1)
  5. Compare a linear search and a binary search, using the list 3, 8, 12, 19, 25, 31, 40 and searching for 31. [6 marks]

    • a linear search checks each item in turn from the start (1)
    • it would check 6 items before finding 31 (1)
    • a binary search only works on a sorted list (this one is) (1)
    • it checks the middle item, 19; 31 is bigger, so the left half is discarded (1)
    • the middle of 25, 31, 40 is 31: found in 2 comparisons (1)
    • binary search is much faster for large lists, halving the list each time (1)
    • linear search works on unsorted lists and is simpler to code (1)

How to answer them: exam technique and command words.

Searching and sorting algorithms: questions and answers

What is one disadvantage of a merge sort compared with a bubble sort?
It uses more memory. It needs extra space for the split lists.
What is one advantage of a linear search?
It works on an unsorted list. Binary search needs the list to be sorted first.
A bubble sort starts on the list 5, 2, 4. What is the list after the first swap?
2, 5, 4. 5 and 2 are compared and swapped first.

Practise Searching and sorting algorithms: 16 questions

Download these notes as a PDF

Previous topic: Pseudocode and flowchartsNext topic: Programming fundamentals

More in Fundamentals of algorithms

LycoLearn is still loading…

This can take a little longer on a busy school or home network. If nothing happens, try refreshing the page.

LycoLearn didn’t load properly

Part of it didn’t arrive. Try refreshing the page in a minute, or open LycoLearn in another browser.