← Course home

Foundations · Lesson 1 of 52

Input size and operation counts

Choose an input-size variable and translate simple loops into operation counts.

Full lessonAbout 9 min

You will learn to choose an input-size variable and count work in simple and nested loops.

On this page 6 sections

The idea

For n = 4 the nested pair scan checks 6 pairs. The exact count is 4 * 3 / 2.

Name the input size before you count work. For one list, n is the number of items. For a graph, say V vertices and E edges instead of one n. An operation is one comparison, one link follow, or one arithmetic step that does not itself hide a loop. A single loop from 0 to n - 1 does n iterations. A nested loop that compares every index with every later index does (n - 1) + (n - 2) + ... + 1 checks. That sum is n(n - 1) / 2. Dropping the constant 1/2 and the lower term is a later step. This lesson keeps the exact count so the next lesson has something real to bound.

A worked trace

StepStateWhat changed
Paircount = 1Compare 3 with 1. The pair count is 1.
Paircount = 2Compare 3 with 2. The pair count is 2.
Paircount = 3Compare 1 with 2. The pair count is 3.

Why it is correct

The count is correct because every pair of indexes i < j is visited once. Index i is the outer loop, and j runs from i + 1 to n - 1. No pair is skipped, and no pair is counted twice. The closed form n(n - 1) / 2 is the formula for the sum of the first n - 1 positive integers. For n = 4 the trace records 3 + 2 + 1 = 6, and 4 * 3 / 2 = 6.

A common mistake

Counting n times n double-counts every pair. The inner loop only looks to the right of i.

What it costs

The pair scan does n(n - 1) / 2 comparisons, which grows like n squared. Memory besides the input is a handful of indexes, a constant amount of extra memory. Suppose the inner loop ran over the whole list. The count would then be n times n. That is the same growth with a different constant.

Run it

python
from bitsandbytes.complexity import pair_comparison_countprint(pair_comparison_count([3, 1, 2]))# prints: 3 

Practice and next step

Open Asymptotic growth next. Asymptotic growth replaces this exact count with a tight bound.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.complexity

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Count the pair checks

Watch a nested scan of four items. The outer index moves once per item. The inner index only looks to the right.

count
1

State

  1. 01chosen

Compare 3 with 1. The pair count is 1.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1A loop over 6 items checks every unordered pair. How many checks run?
2The same pair loop runs on 4 items. How many checks is that?
print(4 * 3 // 2)

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.