Chapter 17: Elementary Sorting Algorithms
17.06Beginner / Intermediate5 topics15 min2 interactive animations

Sorting analysis

The elementary sorts all share an O(n²) worst case, but they behave very differently on real inputs. This lesson compares them side by side, live on whatever input you type.

Learning goals

  • Compare best, average and worst case across elementary sorts
  • Separate comparison cost from data-movement cost
  • Predict behaviour on sorted, reverse and duplicate-heavy input

Prerequisites

Bubble, selection and insertion sortInversions
17.06.1

Best/average/worst

Summary of the algorithms in this chapter. “Adaptive” means faster on nearly sorted input.

AlgorithmBestAverageWorstSpaceStableAdaptive
Bubble (early exit)O(n)O(n²)O(n²)O(1)YesYes
SelectionO(n²)O(n²)O(n²)O(1)NoNo
InsertionO(n)O(n²)O(n²)O(1)YesYes
Binary insertionO(n log n)O(n²)O(n²)O(1)YesPartly
Cocktail shakerO(n)O(n²)O(n²)O(1)YesYes
CombO(n log n)O(n²) boundO(n²)O(1)NoPartly
GnomeO(n)O(n²)O(n²)O(1)YesYes
Shell (n/2 gaps)O(n log n)depends on gapsO(n²)O(1)NoYes
CycleO(n²)O(n²)O(n²)O(1)NoNo
17.06.2

Comparisons vs swaps

Comparisons and data moves cost different amounts. Comparing two ints is cheap, but comparing long strings or calling a complex Comparator is not. Moving a large struct is expensive, but moving a reference is cheap.

Selection sort makes the most comparisons and the fewest swaps. Insertion sort's shifts equal the inversion count. Cycle sort makes at most n writes. Type your own array below and compare the live counts.

Compare every elementary sort on your input

Live counts

Your input

Try

Bubble sort

20 comparisons
11 swaps
No duplicates

Selection sort

21 comparisons
5 swaps
No duplicates

Insertion sort

15 comparisons
11 shifts
No duplicates

Binary insertion sort

12 comparisons
11 shifts
No duplicates

Cocktail shaker sort

21 comparisons
11 swaps
No duplicates

Comb sort

23 comparisons
7 swaps
No duplicates

Gnome sort

26 comparisons
11 swaps
No duplicates

Shell sort

15 comparisons
7 shifts
No duplicates

Cycle sort

47 comparisons
7 writes
No duplicates

Try “Already sorted” and “Reverse sorted” to see adaptive sorts collapse to linear work while selection sort does not change.

17.06.3

Sorted input

Already sorted input has zero inversions. Bubble sort with early exit, insertion, cocktail and gnome sort finish in one linear pass with no moves. Selection sort still performs all n(n-1)/2 comparisons.

In practice this matters a lot: data often arrives nearly sorted, for example timestamps or a list after one append.

17.06.4

Reverse input

Reverse sorted input has the maximum n(n-1)/2 inversions, which is the worst case for bubble and insertion sort: every pair must be fixed. Selection sort makes only ⌊n/2⌋ swaps here, because each swap fixes two positions.

17.06.5

Duplicate keys

Duplicates raise two questions: do equal keys keep their order (stability), and does the algorithm waste work moving equal keys around? Using strict > instead of >= in bubble and insertion sort keeps them stable and avoids useless moves.

When there are only a few distinct keys, counting sort (Chapter 18) or three-way partitioning in quicksort is far faster than any elementary sort.

Stability: what happens to equal keys?

Interactive

Your input

Try

Input · letters show original order of equal keys

3a3b2a2b2c1

Insertion sort

12a2b2c3a3b
a before b

Bubble sort

12a2b2c3a3b
a before b

Selection sort

12a2b2c3b3a
Order broken

Shell sort

12b2c2a3a3b
Order broken

Practice ladder

Now try it yourself

Easy

  • Predict comparisons and swaps of each sort on [1, 2, 3, 4, 5]
  • Which sort minimises swaps on reverse input?

Medium

  • Count inversions in O(n²) and check against insertion sort's shifts
  • Choose a sort for 20 nearly sorted records with expensive comparisons

Advanced

  • Count inversions in O(n log n) using merge sort
  • Design an experiment that measures the cost of comparisons vs moves in Java

Key takeaways

  • All elementary sorts are O(n²) worst case, but adaptivity and stability differ.
  • Count comparisons and moves separately; which one dominates depends on the data type.
  • Insertion sort is the practical winner for small or nearly sorted data.