Algorithms · GCSE Computer Science

Algorithms Playground

Step through the searching and sorting algorithms you need to know. Watch the pointers move, read what happens at each step and see the line of pseudocode that does it.

Searching
Sorting
Data set
Extras

Choose an algorithm to begin

Pick a searching or sorting algorithm above. You'll get a fresh list of 12 items each time.

The standard searching and sorting algorithms

These are the searching and sorting algorithms in the OCR GCSE Computer Science (J277) specification. Choose one above to step through it, or use this summary to revise.

Linear search

Checks each item in turn, from the start, until it finds the one it wants or reaches the end. It works on any list, sorted or not, but it is slow on large lists because it may have to check every item.

Binary search

Only works on a sorted list. It checks the middle item, then throws away the half that can't contain the target, and repeats. It is much faster on large lists: about 20 checks are enough for a million items.

Bubble sort

Compares each pair of neighbouring items and swaps them if they are in the wrong order. It repeats these passes until a pass makes no swaps. It is simple to write but slow for large lists.

Insertion sort

Takes each item in turn and inserts it into the right place in the sorted part of the list to its left. It is efficient for small lists and lists that are nearly sorted.

Merge sort

Splits the list in half again and again until every list has one item, then merges the lists back together in order. It is much faster than bubble sort on large lists, but uses more memory.

AlgorithmNeeds sorted data?StrengthWeakness
Linear searchNoWorks on any listSlow on large lists
Binary searchYesVery fast on large listsList must be sorted first
Bubble sort–Simple to understand and codeSlow on large lists
Insertion sort–Good for small or nearly sorted listsSlow on large unsorted lists
Merge sort–Fast on large listsUses more memory

Frequently asked questions

Why does binary search need a sorted list?

It decides which half to throw away by comparing the target with the middle item. That only works if every item to the left is smaller and every item to the right is larger.

Which sorting algorithm is fastest?

Of these three, merge sort is usually fastest for large lists. Insertion sort can be quicker for small or nearly sorted lists.

What is the exam question button for?

It gives a practice question using the same list, so you can write out the steps yourself and then check them against the walkthrough.