Meaning and assumptions
A sorting algorithm must preserve all original elements and order them under a specified comparison rule. Insertion sort builds a sorted prefix; merge sort splits, sorts subarrays and merges them. Invariants explain correctness, while an explicit cost model explains running time.[1]
Comparison-based sorting of finite sequences. Key comparisons are counted separately from moves. The merge recurrence here assumes powers of two and linear-time merging; Θ bounds are not stopwatch times.[1]
Why the approach works
Before insertion-sort step i, the prefix before position i is sorted and contains the same elements as before. Insert the current element while shifting larger predecessors right. This grows the sorted prefix by one and preserves the multiset. The worst case needs1+2+…+(n−1) moves. With equal halves, merge sort satisfies the stated recurrence: each level merges a linear total number of elements and there are logarithmically many levels. Extra storage, stability and input structure remain separate selection criteria.[1]
Two complete worked examples
Insertion sort with an explicit counting rule
- Given
- Sequence[4,1,3,2]; i starts at1. Count executed key comparisons, not index checks.
- Find
- Result
Insert1 before4; insert3 between1 and4.
Insert2 between1 and3.
Result: Result:[1,2,3,4]. Key comparisons:1+2+3=6; moves:1+1+2=4.
Check: Intermediate arrays are[1,4,3,2] and[1,3,4,2]; every input value remains exactly once.
Merge sort on the same sequence
- Given
- Split[4,1,3,2] into halves and count key comparisons in the usual merge.
- Find
- Result
Sort each half with one comparison.
Merge[1,4] and[2,3] with three further comparisons.
Result: Result:[1,2,3,4] after1+1+3=5 key comparisons.
Check: After choosing1,2 and3, append4 without another key comparison.
Recall the method later with the formula’s flashcards.
10 flashcards · English materials
Already have an account? Sign in for flashcardsAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Five exercises with solutions
Calculate · Application
How many moves does insertion sort need for[4,3,2,1]?
Show hint
State the approach and assumptions before substituting.
Learning goal: How many moves does insertion sort need for[4,3,2,1]?
Show solution and calculation
1+2+3=6 right shifts in the specified insertion variant.
Calculate · Application
How many key comparisons does the same variant need for[1,2,3,4]?
Show hint
State the approach and assumptions before substituting.
Learning goal: How many key comparisons does the same variant need for[1,2,3,4]?
Show solution and calculation
One unsuccessful greater-than comparison in each of three steps:3.
No elements move.
Interpret · Application
Why does an ascending output alone not prove a correct sort?
Show hint
State the approach and assumptions before substituting.
Learning goal: Why does an ascending output alone not prove a correct sort?
Show solution and calculation
The algorithm might lose values or create extras.
Both sortedness and preservation of the original multiset are required.
Error analysis · Application
Does Θ(n log n) mean merge sort is faster on every input?
Show hint
State the approach and assumptions before substituting.
Learning goal: Does Θ(n log n) mean merge sort is faster on every input?
Show solution and calculation
No.
Asymptotics describe growth under a model; small or nearly sorted inputs, constants and implementation details can alter the practical outcome.
Transfer · Challenge
How does insertion sort remain stable for equal keys?
Show hint
State the approach and assumptions before substituting.
Learning goal: How does insertion sort remain stable for equal keys?
Show solution and calculation
Shift only strictly larger predecessors, not equal ones.
A later equal-key element then does not overtake an earlier one.
Check your understanding with the formula’s reference test.
4 test questions · English materials
Already have an account? Sign in for the testAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Related next steps
Sources for this explanation
Quanta · Updated November 6, 2026
Editorial responsibility: Quanta Study editorial team
Review scope
This explanation, 2 worked examples and 5 exercises are available in German and English. Structure, source references and mathematical notation undergo automated checks. Calculations, model assumptions and both language versions receive a separate AI review.
An independent, named human subject review has not yet been documented. Our exercise numbers are worked examples, rather than measurements published by the cited sources.
Editorial responsibility, sources and methodsChange history
- Locally authored DE/EN article with two original examples, five exercises, model limits and a native diagram.
Cite this reference page
Quanta. Sorting algorithms: invariants and running time. Updated November 6, 2026. https://quanta-study.de/en/formula-sheet/algorithmische-grundmuster-sortieren
When citing a specific scientific statement, also cite the specialist source linked to that statement.