GCSE Computer Science · Computational thinking, algorithms and programming

Searching and sorting algorithms

Linear and binary search, and bubble, merge and insertion sort: how each works step by step and when one beats another.

139 GCSE-style practice questions

Watch the lesson

Every point below, worked through in one video, and each point links straight to its own part.

9 minutes. Plays from YouTube.

More lessons like this one on our YouTube channel

Learn

Searching and sorting algorithms, explained point by point

Everything the GCSE specification expects you to be able to do, and how to actually do it - the same lesson a signed-in student studies from.

  1. Carry out a linear search

    A linear search starts at the first item and checks each one in turn until it finds the target or reaches the end of the list. It works on any list, sorted or not, and it is simple to write. Its weakness is speed: in a list of 1000 items, the target might be the last one checked, or not there at all, and either way every item has been looked at.

  2. Carry out a binary search

    A binary search only works on a list that is in order. Look at the middle item. If it is the target, stop. If the target is smaller, throw away the middle item and everything above it, and if it is larger, throw away the middle item and everything below. Repeat on the half that is left. With an even number of items there are two middle positions, so pick one and be consistent. Each step halves the list, so a sorted list of 1000 items needs at most 10 comparisons.

  3. Compare linear search and binary search

    Binary search is far faster on a long list because it halves the search each time, but the list has to be sorted first. Linear search is slower on average but needs no preparation. So for a short list, or one that is not in order, linear search is the sensible choice. For a long list that is already sorted, use binary search. An answer that only says one is faster misses the condition that earns the second mark.

  4. Carry out a bubble sort

    A bubble sort works through the list comparing each pair of neighbours and swapping them if they are in the wrong order. One trip through the list is a pass, and after the first pass the largest item has reached the end. Keep making passes until one is completed with no swaps: the list is then sorted. In an exam, write out the whole list after each pass so that the marker can follow your work.

  5. Carry out a merge sort

    A merge sort has two stages. First, split the list in half, again and again, until every item is in a list of its own. Then merge the lists back together in pairs, each time taking the smaller of the two front items, so that every merged list is in order. It is much faster than a bubble sort on a long list, but it needs extra memory for the lists it creates along the way.

  6. Carry out an insertion sortOCR only

    An insertion sort builds a sorted section at the start of the list, one item at a time. Take the next unsorted item and move it left, past each larger item, until it sits in the right place. Then take the next. It is how most people sort a hand of cards. It is quick on a short list or one that is nearly in order, and slow on a long jumbled one. Only OCR examines insertion sort.

  7. Compare sorting algorithms

    Bubble sort is easy to write and uses almost no extra memory, but it is slow on long lists because it makes so many comparisons. Merge sort is much faster on long lists and takes about the same time whatever order the data starts in, but it uses more memory. To recognise which sort a question shows, look at what changes: neighbours swapping means bubble, a list being split and rejoined means merge, and a sorted section growing from the left means insertion.

Searching and sorting algorithms key terms

The words the specification and the mark schemes use, each defined the way an examiner wants it.

Linear search
A search that checks each item of a list in turn until the target is found or the list ends.
Binary search
A search of a sorted list that checks the middle item and discards the half that cannot hold the target, repeating until it is found.
Bubble sort
A sort that repeatedly compares neighbouring items and swaps them when they are in the wrong order.
Merge sort
A sort that splits a list into single items and then merges them back together in order.
Insertion sort
A sort that takes each item in turn and inserts it into its correct place in a sorted part of the list.
Pass
One complete trip through the list during a sort.
Efficiency
A measure of how much time or memory an algorithm needs to do its job.

Common mistakes in Searching and sorting algorithms

The slips that cost marks year after year, and what to do instead.

  • Using a binary search on a list that is not in order

    Binary search only works on sorted data. Say that the list must be sorted first, or use a linear search.

  • Keeping the middle item in the half that is left

    The middle item has been checked, so discard it along with the half that cannot hold the target.

  • Stopping a bubble sort after one pass

    One pass only guarantees the largest item is at the end. Keep going until a whole pass makes no swaps.

  • Merging lists without keeping them in order

    At each merge compare the front items of the two lists and take the smaller one first, every time.

  • Describing a sort in general terms when asked to show it

    Write out the list after every pass or every merge, using the data in the question.

Practice

Try a Searching and sorting algorithms question

A GCSE-style original question from this topic. Have a go before you open the working - deciding on an answer first is what makes the working stick.

The list 4, 2, 7, 1, 3 is sorted into ascending order with a bubble sort. What is the list after the second pass?

Make Searching and sorting algorithms stick

A free account adds worked examples from our question bank, from a warm-up to a stretch, plus ask-Wiz - a coach you can put any point from this lesson to - and a checklist that remembers which points you've studied. No card needed.

Ready to practise Searching and sorting algorithms?

Sign up free to practise this topic - questions that adapt to how you're doing, with instant encouraging feedback on every answer. No card needed.

Download on the App StoreGet it on Google Play

Already have an account? Log in

More Computational thinking, algorithms and programming topics