Insertion vs Selection Sort, explained.
Sorting algorithms impose an order through different comparison and movement procedures, with performance depending on both implementation and input.
Why it happens
Insertion sort places a held key among preceding values; selection sort repeatedly finds a remaining minimum. One outer pass is displayed at a time, with cumulative key comparisons and array writes.
Sorting algorithms impose an order through different comparison and movement procedures, with performance depending on both implementation and input.
Read the result
A pass is an outer-loop operation, not a unit of equal computation. Read key comparisons and writes separately for the same starting list.
A worked example
One list, two cost ledgers
Step through insertion sort on seven, two, nine, one, five and three.
Restart selection sort on the identical initial order and inspect its comparison and write totals.
Compare sorted outputs and cost conventions without inferring runtime from the number of displayed passes.
OPTIONAL DEEPER DETAILGo deeper: inside the model
Inside this model
Insertion sort places a held key among preceding values; selection sort repeatedly finds a remaining minimum. One outer pass is displayed at a time, with cumulative key comparisons and array writes.
Where this idea is useful
A practical use
Choose a cost measure that matches expensive comparisons or expensive storage writes rather than judging an animation.
A common misconception
“The algorithm with fewer visible passes necessarily did less work.”
A pass can contain different amounts of searching and moving. The board counts two concrete operations rather than treating animation steps as work.
What this explanation leaves out
- One six-value workload and explicit write conventions are used. Counts are not browser-time benchmarks or a proof that either algorithm dominates all inputs.
Why count a held-key write even when its position stays the same?
That is the stated insertion implementation. Explicit conventions make comparisons auditable instead of silently changing the accounting.
Which operation is actually expensive in the environment where you will sort?
Associated thinkers
Associations marked provisional are awaiting source review.
Further reading
Explore the original research or the teaching reference behind this experiment.