Comparison Lab
Bubble sort vs insertion sort
On the same array, how many element comparisons and array writes does each sort make, and how does that change between sorted, shuffled and reversed input?
No wall-clock benchmarks: every number on this page is an operation count taken from the algorithms' traces, never a timing.
What is counted
- Element comparisons
- One evaluation of an order test between two array elements: a[i + 1] < a[i] (bubble), a[j] > key (insertion), left ≤ right (merge). Loop and index tests are not counted.
- Element writes
- One store of an element into an array slot, in the input array or in an auxiliary buffer. A swap is 2 writes; a shift is 1; placing the key is 1 (even if it lands where it started); merge sort writes each element into the buffer and back. Values held only in local variables (a swap temporary, the held key) are not counted.
- Extra element slots
- Element slots allocated besides the input array.Bubble sort: not applicable. Sorts in place: the trace allocates no element storage.Insertion sort: not applicable. Sorts in place: the trace allocates no element storage.
- Bubble sort: With the early exit: stops after a pass without swaps.
- Insertion sort: Lifts the key, shifts larger elements right, places the key.
- Merge sort (optional lane): Top-down, one auxiliary buffer of n slots.
Totals for every preset
Computed when this page was built, by running each algorithm on the preset (seed 1). The lab above replays the same traces.
| Preset | Metric | Bubble sort | Insertion sort | Merge sort |
|---|---|---|---|---|
| Shuffled | Element comparisons | 25 | 20 | 17 |
| Element writes | 30 | 22 | 48 | |
| Extra element slots | n/a | n/a | 8 | |
| Best case: sorted | Element comparisons | 15 | 15 | 32 |
| Element writes | 0 | 15 | 128 | |
| Extra element slots | n/a | n/a | 16 | |
| Worst case: reversed | Element comparisons | 120 | 120 | 32 |
| Element writes | 240 | 135 | 128 | |
| Extra element slots | n/a | n/a | 16 | |
| Nearly sorted | Element comparisons | 29 | 16 | 32 |
| Element writes | 2 | 16 | 128 | |
| Extra element slots | n/a | n/a | 16 | |
| Equal values | Element comparisons | 27 | 16 | 16 |
| Element writes | 22 | 18 | 48 | |
| Extra element slots | n/a | n/a | 8 |
- Shuffled:
- Eight distinct values in no particular order: a typical case for both.
- Best case: sorted:
- 16 values already in order. Both make n − 1 = 15 comparisons. Bubble sort writes nothing (one pass, no swap, early exit); insertion sort still writes every key back into its own slot: 15 writes.
- Worst case: reversed:
- 16 values in reverse order. Both make n(n − 1)/2 = 120 comparisons. Bubble sort writes 2 per swap (240); insertion sort writes 1 per shift plus 1 per placement (120 + 15 = 135).
- Nearly sorted:
- One neighbouring pair out of order. Insertion sort does almost no extra work; bubble sort needs a second, swap-free pass to notice it is done.
- Equal values:
- Repeated values. Neither sort moves an element past an equal one, so both stay stable.
What to look for
On reversed input both sorts compare every pair once, so the comparison counts match. The difference is in writes: a bubble-sort swap stores two elements, an insertion-sort shift stores one, and the held key is written once per pass.
On sorted input bubble sort is the cheaper of the two in writes (zero), because of its early exit. Insertion sort still places each key, which is one write per element.
Add merge sort to see the other side of the trade-off: far fewer comparisons on large shuffled or reversed input, but 2 writes per element per level of the recursion and an n-slot buffer, even when the input is already sorted.
Each side on its own, with code in four languages: Bubble sort, Insertion sort, Merge sort.