Revise Computing
Revision notesQuestionsFor schools
Log inStart revising free
Revision notesQuestionsFor schoolsLog in
GCSE Computer Science/Paper 2/Searching and sorting algorithms

Insertion sort

Standard sorting algorithms: Insertion sort

2.1.3 Searching and sorting algorithms

Practise exam questions

Searching and sorting algorithms — Note 5 of 5

Understand the main steps of each algorithm, Understand any pre-requisites of an algorithm, Apply the algorithm to a data set, Identify an algorithm if given the code or pseudocode for it

What you need to know

Insertion sort works by building a sorted section of the list one item at a time. Starting from the second item, each element is compared to those already sorted and inserted into its correct position. It is simple to understand and efficient for small to medium-sized lists, but slower than merge sort for very large datasets.

What to know

  • The array has two sections: a SORTED section (left) and an UNSORTED section (right). The sorted section builds from the START.
  • Start at the second item (index 1) since a single item is already 'sorted'.
  • Compare the current item to items on its left and insert it into the correct position.
  • A TEMP variable is used to store the value being moved, so it is not lost/overwritten during the swap process.
  • The OUTER loop is count-controlled (for loop) — it visits each item once.
  • The INNER loop is condition-controlled (while loop) — because we don't know how many swaps/moves are needed to place the item correctly.
  • The condition-controlled loop is more EFFICIENT than a count-controlled loop — it stops as soon as the correct position is found, rather than iterating unnecessarily.
  • Completes sorting in a SINGLE PASS through the array (the outer loop).
  • Efficient for small or nearly-sorted lists. Generally more efficient than bubble sort on average.
  • Less efficient than merge sort for large, unsorted datasets.

Exam technique

  • Tip:Explain WHY the inner loop is condition-controlled: we do not know at run-time how many swaps/positions the item needs to move. It repeats WHILE the current item is smaller than the one to its left.

Code examples

Insertion sort in Python
python
def insertion_sort(data):
    for i in range(1, len(data)):
        key = data[i]
        j = i - 1
        while j >= 0 and data[j] > key:
            data[j + 1] = data[j]
            j -= 1
        data[j + 1] = key
    return data

print(insertion_sort([5, 3, 9, 4, 7]))
# Output: [3, 4, 5, 7, 9]

Ready to test yourself?

Practise 2.1.3 questions
← Previous noteMerge sort2.1.3 Searching and sorting algorithmsNext topic →Variables, constants, operators, inputs, outputs and assignments2.2.1 Programming Fundamentals

Back to Searching and sorting algorithms

Revise another OCR GCSE topic: Designing, creating and refining algorithms

Searching and sorting algorithms

Note 5 of 5

  1. 1Binary search
  2. 2Linear search
  3. 3Bubble sort
  4. 4Merge sort
  5. 5Insertion sort
Topic overview

© 2026 Revise Computing · revisecomputing.co.uk

Privacy noticeTermsContact

Revise Computing is an independent revision resource. It is not affiliated with, or endorsed by, OCR (Oxford Cambridge and RSA Examinations).