CS11-06 Computer Science Watch
Insertion sort
In this lesson
Insertion sort: how to describe and apply an insertion sort, with worked examples and the mistakes examiners report. OCR J277 GCSE. Searching and sorting 6 of 7. More at https://scholafly.com
By the end: Describe and apply an insertion sort - take each item in turn from the unsorted part and move it left, past every larger item, until it sits in its correct place in the sorted part - explain why the inner loop is condition-controlled, and give one difference and two similarities between insertion sort and bubble sort.
What it covers
- 0:00 Intro: insertion sort
- 0:29 Insertion sort
- 3:24 Insertion sort in code
- 5:26 Bubble sort or insertion sort
Key words
About this video
Insertion sort | GCSE Computer Science | CS11-06
Insertion sort: how to describe and apply an insertion sort, with worked examples and the mistakes examiners report.
OCR J277 GCSE. Searching and sorting 6 of 7.
More at https://scholafly.com
Watch first: CS11-04 - https://youtu.be/iM0KFXiNPp0
Video code: CS11-06 (search "ScholaFly CS11-06")
#GCSEComputerScience #SearchingAndSorting #InsertionSort
For teachers
This GCSE Computer Science lesson teaches insertion sort. By the end, students should be able to describe and apply an insertion sort - take each item in turn from the unsorted part and move it left, past every larger item, until it sits in its correct place in the sorted part - explain why the inner loop is condition-controlled, and give one difference and two similarities between insertion sort and bubble sort. It works through three worked examples and the mistakes examiners report.
Exam board specification references:
OCR J277
- 2.1.3
Read the transcript
You are dealt cards one at a time and keep your hand in order. Does every new card slide the same distance? Saying yes, one place each, is tempting. But one card may not move at all, and the next may pass the whole hand. And that decides what kind of loop a computer needs to do the sliding.
This sort is on one course's list and not on the others. If it is not on yours, everything here is optional, though the comparison with bubble sort at the end is still useful. An insertion sort keeps the list in two parts: a sorted part on the left and an unsorted part on the right. At the start, the first item on its own counts as the sorted part, because a list of one is already in order. Each round takes the next item from the unsorted part and compares it with its left neighbour. If the neighbour is larger, the neighbour shifts one place right, and the comparison repeats. The moment the left neighbour is smaller, or the item reaches the front, it drops into the gap. The sorted part is now one item longer. The list is five, two, eight, one, six, and the sort puts it in ascending order. Round one takes two. Five is larger, so five shifts right, and two reaches the front: two, five, eight, one, six. Round two takes eight. How many places does eight move? None. Its left neighbour, five, is smaller, so eight stays where it is after a single comparison. Round three takes one. Eight shifts right, then five, then two, and one reaches the front: one, two, five, eight, six. That round needed three shifts. Look at the moves so far: one for two, none for eight, three for one. The number of places moved changes from round to round. Round four takes six. Where does six stop, and what is the final list? Next to five. Eight shifts right, five is smaller, so six drops in: one, two, five, six, eight.
In code, an insertion sort has two loops, and each one has its own job. The outer loop picks the next item from the unsorted part. The inner loop slides that item left, shifting each larger item one place to the right. A count-controlled loop runs a set number of times. A condition-controlled loop, like a WHILE loop, keeps going until a condition changes. Why is the inner loop condition-controlled rather than count-controlled? Nobody knows in advance how many places the item must move. The loop stops when the item is in the right position, the way you stop walking along a queue at your own spot. The rounds showed it: one shift, then none, then three, then one. A count that keeps changing, so no fixed count can be written into the loop. One examiner's report, on a question about this inner loop, says: Candidates generally found this question challenging. Many responses simply repeated the question and discussed sorting values. More successful responses understood that the inner loop is the part of the algorithm that moves the name repeatedly in the list and that we do not know how many times this move needs to be made. The fix is to name the job, not the topic. Name what the inner loop does, moving the item along, and why its moves cannot be counted in advance.
Bubble sort and insertion sort can look alike, so it helps to run both on the same list. On five, two, eight, one, six, one bubble sort pass gives two, five, one, six, eight, with the largest item carried to the end. One round of insertion sort gives two, five, eight, one, six, with a sorted part starting to grow from the front. Now three sentences, each claiming a difference. A, bubble sort needs extra lists, while insertion sort works inside the one list. B, bubble sort settles the end of the list first, while insertion sort grows a sorted part from the front. C, bubble sort compares neighbouring items, while insertion sort never does. Which of the three sentences is a true difference? B. A and C are false: neither sort needs extra lists, and both compare neighbouring items. That gives a safe answer to a compare question: one difference, where each builds its order from, and two similarities, both compare neighbours and both sort inside one list, with no extra lists.
A last run at the rounds and the inner loop, with new lists. Insertion sort on four, one, three: what is the list after round one? One, four, three. Four is larger than one, so four shifts right and one reaches the front. Now round two: how many places does three move? One place. It passes the item to its left, then meets one, which is smaller, and stops. Here's a sorted list, two, four, six, eight: how many shifts in total? Zero shifts. Every value already sits beside a smaller neighbour, so every inner loop ends after one comparison. One more, the hand of cards: why can't the sliding loop run a set number of times? Each new card needs a different number of slides, and nobody knows how many until it reaches its gap.
If you have cracked it, leave a thumbs up, and the list of what you have covered gets one shorter. If not, save it; this one often lands on a second viewing, once the card picture has settled.
Next in the chapter: Bubble or merge, which and why. Hopefully that's next in your feed, but if not, search for ScholaFly C S eleven, oh seven.
Every other video is on scholafly.com too, easy to search by video code or by keyword. Whatever you're revising, go there and have a look, or, watch the next video.
Related terms
For: OCR GCSE J277
On the specification
| Board | Spec | Statement |
|---|---|---|
| OCR GCSE J277 | 2.1.3 | Searching and sorting algorithms |
For teachers
This GCSE Computer Science lesson teaches insertion sort. By the end, students should be able to describe and apply an insertion sort - take each item in turn from the unsorted part and move it left, past every larger item, until it sits in its correct place in the sorted part - explain why the inner loop is condition-controlled, and give one difference and two similarities between insertion sort and bubble sort. It works through three worked examples and the mistakes examiners report.