Skip to content
Lab

Race the sorts

Algorithms (up to 4)
Input shape

The race

Every algorithm sorts the same 16 values. The clock counts work (comparisons, swaps and writes), not seconds, so the one that needs the fewest operations finishes first.

0 operations
Bubble SortO(n²)Running
Array

Array: 2, 7, 97, 70, 52, 41, 47, 24, 55, 73, 26, 16, 76, 52, 20, 37

0 / 173 ops
Insertion SortO(n²)Running
key

key: empty, 7, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty

Array

Array: 2, empty, 97, 70, 52, 41, 47, 24, 55, 73, 26, 16, 76, 52, 20, 37

0 / 148 ops
Merge SortO(n log n)Running
Array

Array: 2, 7, 97, 70, 52, 41, 47, 24, 55, 73, 26, 16, 76, 52, 20, 37

merge buffer B

merge buffer B: empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty, empty

0 / 173 ops
Quick SortO(n log n)Running
Array

Array: 2, 7, 97, 70, 52, 41, 47, 24, 55, 73, 26, 16, 76, 52, 20, 37

0 / 59 ops

How the work grows

Comparisons + swaps + writes for random input as n goes from 4 to 64. The thin curves are n²/2 and n·log₂n for reference.

  • Bubble Sort
  • Insertion Sort
  • Merge Sort
  • Quick Sort
  • n²/2 (reference)
  • n log₂ n (reference)
01K2K3K41220283644526064input size nBubble Sortn²/2Merge SortQuick Sort
At n = 64

Bubble Sort makes about 2,900 operations. Quick Sort makes about 560.

Double n from 32 to 64 and bubble sort's work grows ×4.1; quick sort's grows ×2.5.

Why: Big-O and growth