Skip to the content
Software Made Clear Diagrams that show the mechanism About

VISUALISERS · FOUNDATIONS

← All visualisers

Sorting algorithm visualiser

Sorting answers which algorithm, and on what input?

52917310486Insertion sortComparisons: 0

COMPARISONS

0

Nothing compared yet. Take a step to start counting.

“Step” advances one comparison; “Run” plays out the rest of them. “Shuffle” deals the same ten values into a new random order, which is the middling case for both algorithms. Switching algorithm restarts at step zero, so the two counts are always measured over the same array.

Where the two cross over

A shuffled array is the only case the control above produces, and it is the one where the two algorithms come out close. The two figures below are fixed: the same ten values handed to insertion sort twice, once nearly sorted and once reversed, each shown at its last step.

Nearly sorted, insertion sort makes 10 comparisons and merge sort 21. Reversed, insertion sort makes 45 and merge sort 15. Merge sort's two counts, 21 and 15, sit close together; insertion sort's, 10 and 45, do not.

Nearly sorted

12345678910Insertion sortComparisons: 10
Insertion sort, finished. It started from 1, 2, 3, 4, 5, 6, 7, 8, 10, 9: one pair out of order.

INSERTION SORT

10 comparisons

MERGE SORT

21 comparisons

Reversed

12345678910Insertion sortComparisons: 45
Insertion sort, finished. It started from 10, 9, 8, 7, 6, 5, 4, 3, 2, 1: every pair out of order.

INSERTION SORT

45 comparisons

MERGE SORT

15 comparisons

Both figures end with the same ten bars in the same order. What differs is the number in the heading — what it cost to get there.

Want an explanation to go with the picture? Find a guided starting point →