Card 07/ 08

ExerciseDifficulty: Intermediate1 min

Practice: Classifying Costs and Checking a Time Limit

Simplify four cost expressions, rank them, then decide which one fits a stated input size.

Classifying costs takes three skills used together: simplifying a cost to its class, ranking classes by growth, and checking a class against a time limit. This exercise practises all three. Four algorithms for the same task cost these numbers of steps on n items:

  • A: 5n + 3n log n
  • B: n²/2 + 100n
  • C: 2ⁿ + n³
  • D: 7 log n + 40
  1. Simplify each cost to its Big O class.
  2. Rank the four from slowest-growing to fastest.
  3. The input has n = 1,000,000 items and the limit is one second. At 10⁸ simple steps a second, which algorithms fit?
Show the answers
The four costs simplified, ranked and checked at n = 1,000,000; step counts and times printed by a run
CostClassRankSteps at n = 1,000,000SecondsFits in one second
D: 7 log n + 40O(log n)1180Under 0.001Yes
A: 5n + 3n log nO(n log n)264,794,7060.648Yes
B: n²/2 + 100nO(n²)35 × 10¹¹5,001No
C: 2ⁿ + n³O(2ⁿ)4A 301,030-digit numberA 301,022-digit numberNo

In A, 3n log n outgrows 5n. In B, n²/2 outgrows 100n, and the half is a constant factor. In C, 2ⁿ outgrows n³: the run found 2ⁿ the larger for every n from 10. In D, the 40 is a constant.

A fits, but look at its time: 0.648 seconds, against 0.199 for n log n alone. The constants 5 and 3 more than tripled the estimate, so near a limit, work the estimate out with its constants as well as its class. B needs 5,001 seconds, well over an hour.