Count the simple operations a piece of code performs as the input grows.
A step inside a loop runs once for every pass, so nested loops multiply the passes and loops one after the other add them.
Use it when you must say how much work code does, or whether it fits a time limit. Signal words: how many times does this line run, a loop inside a loop, keeps halving the input.
Start: pick one basic step, such as a comparison or an addition, and a counter that starts at zero.
Step: put the counter on the basic step. Inside a loop it runs once per pass. Nested loops multiply the passes, and loops one after the other add them.
Stop: read the counter after the code finishes, for several sizes, and compare with the formula you expect.
It does not tell you the time by itself. It assumes every counted step costs about the same, and it ignores work hidden inside a call.
Cost: counting by running costs as much as the code itself. The formula is what lets you skip the run: n(n-1)/2 for the pair loop, floor(log2 n) for the halving loop.
def count_pair_checks(n):
# [1] Start the counter at zero: nothing has been checked yet.
checks = 0
# [2] Take each item in turn as the first of a pair.
for a in range(n):
# [3] Pair it only with the items after it, so each pair is met once.
for b in range(a + 1, n):
# [4] One check for this pair.
checks += 1
# [5] Return the number of checks the loops made.
return checkspublic long countPairChecks(int n) {
// [1] Start the counter at zero: nothing has been checked yet.
long checks = 0;
// [2] Take each item in turn as the first of a pair.
for (int a = 0; a < n; a++) {
// [3] Pair it only with the items after it, so each pair is met once.
for (int b = a + 1; b < n; b++) {
// [4] One check for this pair.
checks++;
}
}
// [5] Return the number of checks the loops made.
return checks;
}long long countPairChecks(int n) {
// [1] Start the counter at zero: nothing has been checked yet.
long long checks = 0;
// [2] Take each item in turn as the first of a pair.
for (int a = 0; a < n; a++) {
// [3] Pair it only with the items after it, so each pair is met once.
for (int b = a + 1; b < n; b++) {
// [4] One check for this pair.
checks++;
}
}
// [5] Return the number of checks the loops made.
return checks;
}function countPairChecks(n) {
// [1] Start the counter at zero: nothing has been checked yet.
let checks = 0;
// [2] Take each item in turn as the first of a pair.
for (let a = 0; a < n; a++) {
// [3] Pair it only with the items after it, so each pair is met once.
for (let b = a + 1; b < n; b++) {
// [4] One check for this pair.
checks++;
}
}
// [5] Return the number of checks the loops made.
return checks;
}def count_halving_steps(n):
# [1] Start the counter at zero.
steps = 0
# [2] Keep going while there is more than one item left.
while n > 1:
# [3] Cut the input in half, rounding down.
n //= 2
# [4] One pass is done.
steps += 1
# [5] Return the number of passes.
return stepspublic int countHalvingSteps(int n) {
// [1] Start the counter at zero.
int steps = 0;
// [2] Keep going while there is more than one item left.
while (n > 1) {
// [3] Cut the input in half, rounding down.
n /= 2;
// [4] One pass is done.
steps++;
}
// [5] Return the number of passes.
return steps;
}int countHalvingSteps(int n) {
// [1] Start the counter at zero.
int steps = 0;
// [2] Keep going while there is more than one item left.
while (n > 1) {
// [3] Cut the input in half, rounding down.
n /= 2;
// [4] One pass is done.
steps++;
}
// [5] Return the number of passes.
return steps;
}function countHalvingSteps(n) {
// [1] Start the counter at zero.
let steps = 0;
// [2] Keep going while there is more than one item left.
while (n > 1) {
// [3] Cut the input in half, rounding down.
n = Math.floor(n / 2);
// [4] One pass is done.
steps++;
}
// [5] Return the number of passes.
return steps;
}This version reads "two nested loops" and says n × n without looking at the inner loop.
This version starts the inner loop at a, not at a + 1. It checks each id against itself too.
This version says a loop that halves n runs ceil(log2 n) times.
This version tests if id in seen on a list inside a loop over 1,000 ids and counts 1,000 steps.
Counting steps assumes each step costs about the same. A line that adds two numbers and a line that sorts a list of 1,000 numbers are each one line, yet the sort does thousands of steps inside.
systemdesignmastery.dev/dsa/cheatsheets/counting-operations