Six original Cambridge-style questions on insertion sort and comparing sorting algorithms.
FOR i ← 2 TO n, Key ← List[i], j ← i - 1.j >= 1 and List[j] > Key, shift right and decrease j.List[j + 1] ← Key after the inner loop.Describe how an insertion sort puts an array of integers into ascending order.
Using the insertion sort in this lesson (adapted for 5 elements), show the contents of the array after each pass when it starts as 31, 18, 24, 5, 40.
Names : ARRAY[1:200] OF STRING is a global array. Write pseudocode for a procedure SortNames() that uses an insertion sort to put the names into alphabetical order.
ENDPROCEDURE, with local variables declared (i, j, Key : STRING). ✓FOR i ← 2 TO 200 ... NEXT i. ✓Key ← Names[i] and j ← i - 1 at the start of each pass. ✓WHILE j >= 1 AND Names[j] > Key (or an equivalent flag-based loop). ✓Names[j + 1] ← Names[j] and j ← j - 1. ✓Names[j + 1] ← Key. ✓Calculate the number of comparisons an insertion sort makes on 20 items (a) already in ascending order (b) in descending order. Show your working for (b).
A shop keeps a sorted array of 5000 product records. Each evening about 10 new records are added at the end and the array is sorted again. Explain why an insertion sort is a good choice here, and how its performance would change if the array were in reverse order.
Both the insertion sort and the bubble sort are described as sorting in place. Explain what this means for the memory they use.
Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.