Trace Insertion Sort
Build a sorted prefix and preserve all supplied items.
Supplied materials
- Paper and pencil; an adult may read aloud. No device, account or purchase is needed.
Learn the idea
Binary search requires sorted input. Sorting is a separate preparation step with its own work. In insertion sort, treat the first item as a sorted prefix; take the next item as key, move larger prefix items one place right, and insert key in the open place. After each pass the prefix grows by one item.
Use fictional codes [6, 2, 5, 3]. On the first pass, key 2 goes before 6. On the second, key 5 goes between 2 and 6. On the third, key 3 goes between 2 and 5. We compare with strict greater-than while shifting, so equal items need not move past each other.
- Label the initial one-item sorted prefix [6].
- Insert key 2 and write the full array after the pass.
- Insert 5, then 3, recording each growing sorted prefix.
- Check that the final array is increasing and contains the same four codes.
See the method in action
Arrays after passes: [2, 6, 5, 3] → [2, 5, 6, 3] → [2, 3, 5, 6]. The final array is sorted and its items match the original multiset. There are 1 + 1 + 2 = 4 right-shifts of larger items.
Check your reasoning
Your answer is temporary and is not saved to an account. Download your current answer before leaving or reloading. Written work on paper or in another app stays in your own file.
See the answer and explanation
[4, 7, 6]
Only key 4 has been inserted into the initial prefix [7]. The last item 6 is not processed until the next pass.
Take it further
Change the input to [6, 2, 5, 5]. Expected final array: [2, 5, 5, 6]; shift counts by pass are 1, 1, 1, totaling 3. Strict > does not shift an equal 5.
Nothing here is saved to an account or sent to a teacher. The answer download includes your current choice and any checked feedback.
Check the subject sources
- CS Unplugged, University of Canterbury: searching algorithms
- Python documentation: loops, conditions and functions
- Python documentation: sorting concepts and stable ordering
These LifeStarter passages, scenarios and exercises are authored for this Academy with AI assistance and review. External resources keep their publishers’ terms. A grade label suggests a starting level; it does not establish alignment with your school’s complete requirements.