66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.
Or take a quick quiz: 10 questions on this subtopic in GCSE Computer Science (OCR), marked as you tap.
An insertion sort builds up a sorted part of the list one item at a time, taking each item in turn and inserting it into the correct place in the sorted part. You need to show the list after each insertion, describe the steps, recognise the code and compare it with bubble sort and merge sort.
Key facts
First item = sorted part; start by inserting the second item
Take the next item; shift larger sorted items one place right; insert it into the gap
n items need n − 1 insertions
After each insertion, the sorted part at the start grows by one
Pro: simple, in place (little memory), fast for small or nearly sorted lists
Con: slow for large, unsorted lists
Differs from bubble sort: builds a sorted part, rather than swapping adjacent pairs in passes
Notes
How an insertion sort works (ascending order)
The first item on its own is treated as a sorted part of the list, so start with the second item.
Take the next item from the unsorted part. Compare it with the items in the sorted part, working backwards from the right.
Move each sorted item that is larger one place to the right, then insert the item into the gap.
Repeat until the last item has been inserted. A list of n items needs n − 1 insertions.
Worked example: 7, 3, 9, 1, 5
diagram
Insert 3: it is smaller than 7, so 7 moves right. List: 3, 7, 9, 1, 5.
Insert 9: it is larger than 7, so it stays. List: 3, 7, 9, 1, 5.
Insert 1: 9, 7 and 3 all move right. List: 1, 3, 7, 9, 5.
Blue: the sorted part. Green: the item just inserted
The code
The outer loop starts at position 1 (the second item). The inner while loop shifts larger items right.
for i = 1 to items.length - 1
current = items[i]
j = i - 1
while j >= 0 AND items[j] > current
items[j + 1] = items[j]
j = j - 1
endwhile
items[j + 1] = current
next i
Advantages and disadvantages
Advantages: simple to code; sorts in place, so it uses very little extra memory; quick on small lists and on lists that are already nearly sorted, because items already in place need only one comparison.
Disadvantage: slow on large lists that are in a random order, because of the number of comparisons and shifts.
How to answer each type of question
Show the list after each insertion
3 to 4 marksGrade 5
Leave the first item alone; insert the second item first.
For each item, find where it belongs in the sorted part at the start.
Write the whole list after each insertion, even if nothing moved.
Example. An insertion sort is used to put 12, 5, 18, 2, 9 into ascending order. Show the list after each item is inserted. [4]
Show the model answerHide the model answer
After inserting 5: 5, 12, 18, 2, 9
(1)
After inserting 18: 5, 12, 18, 2, 9
(1)
After inserting 2: 2, 5, 12, 18, 9
(1)
After inserting 9: 2, 5, 9, 12, 18
(1)
Describe how an insertion sort works
3 marksGrade 5
Say that the first item is the sorted part and the sort starts with the second item.
Say that each item is compared with the sorted part and inserted in the correct place (larger items move right).
Say that this repeats until every item has been inserted.
Example. Describe how an insertion sort puts a list into ascending order. [3]
Show the model answerHide the model answer
The first item is treated as a sorted list, and the sort starts with the second item (1). Each item in turn is compared with the items in the sorted part and inserted into the correct position, with larger items moved one place to the right (1). This repeats until the last item has been inserted (1).
Explain when an insertion sort is a good choice
2 to 3 marksGrade 7
Look at the data: is it small, or nearly sorted already?
Explain why: items already in place need only one comparison, so there are few moves.
Memory: it sorts in place.
Example. A list of 1000 scores is in ascending order. One new score is added to the end, and the list must be sorted again. Explain why an insertion sort is a good choice. [2]
Show the model answerHide the model answer
Every score that is already in place needs only one comparison (1), so the only real work is inserting the new score into its correct position, which needs far fewer comparisons and shifts than sorting a random list (1).
Identify a sorting algorithm from code
1 to 2 marksGrade 6
Outer loop from the second item and an inner loop that shifts items right: insertion sort.
A swapped flag and comparisons of items[i] with items[i + 1]: bubble sort.
Splitting into halves and merging: merge sort.
Example. Name the sorting algorithm in the code below and give one reason for your answer.
for i = 1 to items.length - 1
current = items[i]
j = i - 1
while j >= 0 AND items[j] > current
items[j + 1] = items[j]
j = j - 1
endwhile
items[j + 1] = current
next i
[2]Show the model answerHide the model answer
Insertion sort (1). Each item from the second onwards is stored in current, larger items before it are shifted one place right, and current is placed in the gap (1).
Don’t lose marks
Starting with the first item: it is already the sorted part, so the first insertion is the second item.
Changing the order of the unsorted items that have not been inserted yet.
Leaving out a step where the item stays where it is: the question may still want that list written out.
Describing a bubble sort (swapping pairs in passes) when asked about insertion sort.
More tips
Memory tricks
Think of sorting playing cards in your hand: pick up the next card and slide it into the right place among the cards you already hold.
Check after the k-th insertion: the first k + 1 items should be in order (the rest are untouched).
Spot the algorithm: 'shift and insert' = insertion; 'compare and swap neighbours in passes' = bubble; 'split and merge' = merge.
Exam technique
Write one line per insertion, and check that the items after the sorted part are unchanged.
Use the words 'sorted part', 'insert' and 'correct position' in descriptions.
For a nearly sorted list, insertion sort is usually the best answer; for a very large random list, merge sort.
What each grade needs
What you need to be able to do, from the first marks up to the top grade.
Grade 3
State what an insertion sort doesIt takes each item in turn and inserts it into the correct place in a sorted part of the list.
Grade 5
Show the list after each insertionInsert the second item, then the third, and so on, writing the list after each one.
Grade 5
Describe the steps of an insertion sortStart with the second item, shift larger sorted items right, and insert the item in the gap.
Grade 6
Recognise insertion sort codeSpot an outer loop from the second item and an inner loop that shifts items right.
Grade 7
Explain when insertion sort is a good choiceIt is quick for small lists and lists that are nearly sorted, and uses little memory.
Grade 8
Compare insertion, bubble and merge sortWeigh up speed on large lists, memory use and how sorted the data already is.
Quick recall
Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.
State the list after 2 has been inserted into the sorted part.
2, 7, 9, 4
A list has 9 items. An insertion sort starts by treating the first item as the sorted part. How many items must be inserted into the sorted part before the whole list is sorted?
8
An insertion sort can be used to put a list into descending order. Write True or False.
True
Complete the sentence using words from the box.
sorted unsorted position last swapped
In an insertion sort, each new item is taken from the ________ part of the list and put in its correct ________ in the sorted part.
unsorted; position
Dev uses an insertion sort on the list [9, 4, 6] and ends with [9, 6, 4]. State whether the list was sorted into ascending order or descending order.
Descending
An insertion sort is putting a list into ascending order. An item in the sorted part is larger than the item being inserted. State what happens to the larger item.
It moves one place to the right.
Sample questions
Written for this site in the style of OCR exam questions. They are not taken from real past papers.
Question 1Easy4 marks
(a) A sorting algorithm takes each item in turn and puts it into the correct position in the sorted part of the list. Which algorithm is this? Tick (✓) one box.
[1]
Bubble sort
Insertion sort
Merge sort
Binary search
(b) Describe how an insertion sort works.
[3]
Show the answer and mark scheme
(a)Answer: Insertion sort
(b)
the first item is treated as a sorted list / the sort starts with the second item
each item in turn is taken from the unsorted part of the list
and inserted into the correct position in the sorted part (larger items are moved one place to the right)
this repeats until the last item has been inserted
Question 2Medium6 marks
The algorithm below sorts the array items into ascending order using an insertion sort. Three parts are missing.
01 for i = 1 to items.length - 1
02 current = items[i]
03 pos = i
04 while pos > 0 AND items[pos - 1] > ....(1)....
05 items[pos] = ....(2)....
06 pos = pos - 1
07 endwhile
08 items[pos] = ....(3)....
09 next i
(a) Write the missing code for each of the gaps (1) to (3).
[3]
(b) Explain why the loop on line 01 starts at 1 and not at 0.
[1]
(c) Explain why the condition pos > 0 is needed on line 04.
[2]
Show the answer and mark scheme
(a)Answer: (1) current (2) items[pos - 1] (3) current
(1) current
(2) items[pos - 1]
(3) current
(b)
the first item (position 0) on its own is already a sorted list / there is nothing before it to compare with
(c)
when the current item is smaller than every item in the sorted part, pos reaches 0
without the check, items[pos - 1] would be items[−1], which is outside the array / causes an error
Question 3Hard7 marks
A shop keeps a list of 500 product codes in order. Each day a few new codes are added to the end of the list, and the list is then sorted again.
(a) Explain why an insertion sort is a good choice for sorting this list.
[3]
(b) An insertion sort is used on the list [3, 5, 8, 12, 14, 6]. State the total number of comparisons it makes. Show your working.
[2]
(c) A bubble sort that compares every adjacent pair in each pass, and stops after a pass with no swaps, is used on the same list instead. State the number of passes it makes and the total number of comparisons.
[2]
Show the answer and mark scheme
(a)
most of the list is already in order
each item that is already in the right place needs only one comparison
so only the few new codes need to be moved / very few comparisons and moves are needed overall
it sorts the list in place, so little extra memory is needed
(b)Answer: 8 (1 each for 5, 8, 12 and 14; 4 for 6)