Seneca

Insertion Sort

Computer Science: OCR GCSE, topic 2.1.15

Insertion Sort

Insertion sort is a simple sorting algorithm, which is very intuitive.

The concept

The concept

  • Imagine sorting a deck of cards.
  • You'll keep all the sorted cards at the top of the deck.
  • You'll take the next unsorted card.
  • Compare the card with all the sorted ones from the front.
  • Find the correct place to insert the card.
In English

In English

  • Always assume the first card is sorted, since there are no other values to compare it to.
  • Take the second card and compare it to the one on top.
  • Swap if needed.
  • Take the third card.
  • Compare the card with the one on top, then with the second one.
  • Insert the card in the correct place.
  • Take the fourth card.
  • Compare the card with the one on top, then with the second one, then with the third one.
  • Insert the card in the correct place.
  • Repeat until you have sorted all cards.
In pseudocode

In pseudocode

Test yourself on Insertion Sort

  • Select the correct statement about insertion sort in each pair.

  • In insertion sort, how does inserting the fourth card differ from inserting the second card?

Practise this topic for free