← The collection

Insertion vs Selection Sort.

Sort six values with two algorithms and count comparisons separately from writes.

Interactive experimentintuitiveField note ·
INTERACTIVE EXPERIMENT / P17

Insertion vs Selection Sort

Sort six values with two algorithms and count comparisons separately from writes.

PLAY & EXPLAIN

Sort the same six values under two algorithms. One step completes an outer pass. Compare key comparisons and array writes, rather than assuming the shortest animation is fastest.

Sorting algorithm
729153
Completed passes
0
Key comparisons
0
Array writes
0

Initial order: 7, 2, 9, 1, 5, 3.

Counts follow the displayed implementation: insertion writes a held key even if it stays in place; selection avoids self-swaps. This is one workload, not a runtime benchmark.

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.

THE SHORT VERSION

Insertion vs Selection Sort, explained.

Sorting algorithms impose an order through different comparison and movement procedures, with performance depending on both implementation and input.

01 / THE MECHANISM

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.

02 / FOLLOW IT THROUGH

A worked example

One list, two cost ledgers

  1. Step through insertion sort on seven, two, nine, one, five and three.

  2. Restart selection sort on the identical initial order and inspect its comparison and write totals.

  3. 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.

03 / BEYOND THE EXPERIMENT

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.

CHECK YOUR INTUITION

A common misconception

THE TEMPTING CONCLUSION

“The algorithm with fewer visible passes necessarily did less work.”

THE MORE USEFUL DISTINCTION

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.
ONE MORE QUESTION

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.

TAKE THE IDEA WITH YOU

Which operation is actually expensive in the environment where you will sort?

Associated thinkers

Associated withDonald Knuth ↗

Associations marked provisional are awaiting source review.

Further reading

Explore the original research or the teaching reference behind this experiment.