Back

Big-O empirical scaling

What you are seeing: the same shuffled array sorted by two algorithms at once. The left panel runs an $O(N^2)$ comparison sort, the right panel runs merge sort, $O(N \log_2 N)$. Every comparison is counted live. The lower panel plots each finished race as a point on top of the theoretical $\tfrac{1}{2}N(N-1)$ and $N \log_2 N$ curves: the measured counts land on the predicted curves, so the abstract complexity plot is the mechanism you just watched. Press Sweep to fill the curve across many $N$.

Figure 1. Empirical vs theoretical comparison-sort complexity. Method: instrumented bubble/insertion vs merge sort on a seeded shuffle; measured comparison counts overplotted on the asymptotic curves.
array size N128
speed2
O(N^2) sortbubble

WHAT TO TRY

  • Press Sweep N to fill the complexity plot across many array sizes at once.
  • Switch the $O(N^2)$ algorithm between bubble and insertion sort and compare the comparison counts.
  • Raise $N$ and watch the gap between the quadratic and $N\log N$ curves widen.