Describe how running time grows with input size, ignoring constants.
Compare how the work grows when n doubles, and keep only the fastest-growing term without its constant.
Use it when you must compare methods by how they scale, or say whether a method will fit when the input grows. Signal words: how does the work grow, ignore the constant factor, looks fast on a small input.
Start: count the steps at two sizes, n and 2n, with a counter on the basic step.
Step: divide the larger count by the smaller one. About 1 means constant, about 2 linear, a little over 2 n log n, about 4 quadratic, about 8 cubic.
Stop: keep the fastest-growing term of the formula, drop its constant, and write O of that term.
It does not say which method is faster at a given n. A method with a big constant can be slower at every size you will ever see, and a ratio near 2 needs a third size.
Cost: measuring costs two runs. The formula lets you skip the runs: keep the leading term and drop the rest.
def growth_label(steps_before, steps_after):
# [1] Measure how many times bigger the work got when n doubled, in hundredths.
ratio = steps_after * 100 // steps_before
# [2] Barely any more work: the count does not depend on n.
if ratio < 130:
return "constant"
# [3] About twice the work: linear.
if ratio < 210:
return "linear"
# [4] A little over twice the work: n log n.
if ratio < 300:
return "n log n"
# [5] About four times the work: quadratic.
if ratio < 600:
return "quadratic"
# [6] Eight times or more: cubic or worse.
return "cubic or worse"public String growthLabel(long stepsBefore, long stepsAfter) {
// [1] Measure how many times bigger the work got when n doubled, in hundredths.
long ratio = stepsAfter * 100 / stepsBefore;
// [2] Barely any more work: the count does not depend on n.
if (ratio < 130) {
return "constant";
}
// [3] About twice the work: linear.
if (ratio < 210) {
return "linear";
}
// [4] A little over twice the work: n log n.
if (ratio < 300) {
return "n log n";
}
// [5] About four times the work: quadratic.
if (ratio < 600) {
return "quadratic";
}
// [6] Eight times or more: cubic or worse.
return "cubic or worse";
}string growthLabel(long long stepsBefore, long long stepsAfter) {
// [1] Measure how many times bigger the work got when n doubled, in hundredths.
long long ratio = stepsAfter * 100 / stepsBefore;
// [2] Barely any more work: the count does not depend on n.
if (ratio < 130) {
return "constant";
}
// [3] About twice the work: linear.
if (ratio < 210) {
return "linear";
}
// [4] A little over twice the work: n log n.
if (ratio < 300) {
return "n log n";
}
// [5] About four times the work: quadratic.
if (ratio < 600) {
return "quadratic";
}
// [6] Eight times or more: cubic or worse.
return "cubic or worse";
}function growthLabel(stepsBefore, stepsAfter) {
// [1] Measure how many times bigger the work got when n doubled, in hundredths.
const ratio = Math.floor((stepsAfter * 100) / stepsBefore);
// [2] Barely any more work: the count does not depend on n.
if (ratio < 130) {
return "constant";
}
// [3] About twice the work: linear.
if (ratio < 210) {
return "linear";
}
// [4] A little over twice the work: n log n.
if (ratio < 300) {
return "n log n";
}
// [5] About four times the work: quadratic.
if (ratio < 600) {
return "quadratic";
}
// [6] Eight times or more: cubic or worse.
return "cubic or worse";
}def leading_share(n):
# [1] The biggest term: 3 times n squared.
lead = 3 * n * n
# [2] The whole work: the biggest term plus the lower terms.
total = lead + 5 * n + 20
# [3] The share of the biggest term, in whole percent.
share = lead * 100 // total
# [4] Return the share.
return sharepublic int leadingShare(int n) {
// [1] The biggest term: 3 times n squared.
long lead = 3L * n * n;
// [2] The whole work: the biggest term plus the lower terms.
long total = lead + 5L * n + 20;
// [3] The share of the biggest term, in whole percent.
long share = lead * 100 / total;
// [4] Return the share.
return (int) share;
}int leadingShare(int n) {
// [1] The biggest term: 3 times n squared.
long long lead = 3LL * n * n;
// [2] The whole work: the biggest term plus the lower terms.
long long total = lead + 5LL * n + 20;
// [3] The share of the biggest term, in whole percent.
long long share = lead * 100 / total;
// [4] Return the share.
return (int)share;
}function leadingShare(n) {
// [1] The biggest term: 3 times n squared.
const lead = 3 * n * n;
// [2] The whole work: the biggest term plus the lower terms.
const total = lead + 5 * n + 20;
// [3] The share of the biggest term, in whole percent.
const share = Math.floor((lead * 100) / total);
// [4] Return the share.
return share;
}This version runs the three handlers on 10 events and picks the one with the fewest steps.
This version says n²/1000 is tiny, so it drops it and keeps 1000n.
This version says a loop that divides by 2 and a loop that divides by 10 are different classes.
This version says O(n) is always faster than O(n²).
Two methods with the same Big-O can differ by a factor that matters. Handler A makes 3n + 40 steps, and a linear handler with 30n + 400 steps is ten times slower. Both are O(n).
systemdesignmastery.dev/dsa/cheatsheets/big-o-as-growth