← All tools Search & Sorting Algorithms – Part 1

Search & Sorting Algorithms – Part 2

Two more sorting algorithms for GCSE OCR Computer Science: Insertion Sort and Merge Sort — with animated step-by-step walkthroughs, a step-ordering drag-and-drop, and a timed 12-question quiz.

Quick Overview

The essentials for each algorithm, in your own words.

Insertion Sort

Insertion sort builds up a sorted list one item at a time. It takes the next item — the key — and slides it backwards past any bigger items already in the sorted part of the list, until it reaches its correct place.

Sorts in place Worst case: O(n²)

Advantages

  • Simple to understand and implement
  • Fast for small lists, or lists that are already nearly sorted

Disadvantages

  • Slow for large, randomly-ordered lists — O(n²)
  • Still has to shift lots of items on a reverse-sorted list

Merge Sort

Merge sort is a divide and conquer algorithm. It splits the list in half, again and again, until every group has just one item — then merges those groups back together in the correct order.

Divide and conquer Worst case: O(n log n)

Advantages

  • More efficient and quicker than bubble sort
  • Consistent running time — you don't have to backtrack as much

Disadvantages

  • Not good for small lists
  • Still splits the list even if it's already sorted
  • Uses more memory to create the split lists

Insertion Sort — Step by Step

Watch each item get compared with the sorted items before it, then slotted into its correct place.

Show pseudocode
FOR i FROM 1 TO length of list - 1
    SET key TO list[i]
    SET j TO i - 1
    WHILE j >= 0 AND list[j] > key DO
        SET list[j + 1] TO list[j]
        SET j TO j - 1
    END WHILE
    SET list[j + 1] TO key
END FOR

Merge Sort — Split and Merge

Watch the list split all the way down to single items, then merge back together in sorted order.

Show pseudocode
MergeSort(list):
    IF length of list <= 1 THEN
        RETURN list
    END IF
    SET mid TO length of list DIV 2
    SET left TO MergeSort(first half of list)
    SET right TO MergeSort(second half of list)
    RETURN Merge(left, right)

Merge(left, right):
    SET result TO empty list
    WHILE left is not empty AND right is not empty DO
        IF first item of left <= first item of right THEN
            APPEND first item of left TO result
            REMOVE first item from left
        ELSE
            APPEND first item of right TO result
            REMOVE first item from right
        END IF
    END WHILE
    APPEND any remaining items from left TO result
    APPEND any remaining items from right TO result
    RETURN result

Put the Steps in Order

Drag the steps (or click one, then click another to swap them) into the correct order for each algorithm, then check your answer.

Test Yourself — 12 GCSE-Style Questions

OCR-style multiple choice questions on insertion sort and merge sort. A timer starts as soon as you press Start.