AP CSP Topic 3.17 Coding Practice - Algorithmic Efficiency

Big Idea 3: Algorithms and Programming · Topic 3.17 · Coding Practice

Algorithmic Efficiency - Count It Yourself

Count the operations an algorithm actually performs, then watch what happens to that count as the input grows.

You compared growth by eye in Big-O Race. Now count the operations for real. Every problem here is the same question asked louder: how many steps, and what happens to that number when the input gets bigger? Predict the count before you run, then let the computer settle it. The last one is the difference between an algorithm you can ship and one nobody will ever wait for.

Problem 1 of 4

A linear search checks list items one at a time from the front until it finds the target. The list is 4, 8, 15, 16, 23, 42, 7, 9 and the target is 9, which sits at the very end. Count how many items the search has to look at, and print that count.

Expected output: 8

Show AP pseudocode reference
data ← [4, 8, 15, 16, 23, 42, 7, 9]
target ← 9
count ← 0
FOR EACH item IN data
{
  count ← count + 1
  IF(item = target)
  {
    DISPLAY(count)
  }
}
The target is the last item, which is the worst case for a linear search: it has to look at every single element before it finds one. That is why linear search is described as taking n steps on a list of n items.

Problem 2 of 4

Now a nested loop. For a list of 6 items, an algorithm compares every item against every item, including itself. Count how many comparisons that is in total, and print the count.

Expected output: 36

Show AP pseudocode reference
n ← 6
count ← 0
FOR EACH i IN [1..n]
{
  FOR EACH j IN [1..n]
  {
    count ← count + 1
  }
}
DISPLAY(count)
The inner loop runs a full 6 times for each single pass of the outer loop, so the counts multiply rather than add. Six outer passes times six inner passes is the answer, and that multiplying is what makes nested loops quadratic.

Problem 3 of 4

Compare the two directly. For n = 10 and then n = 100, print the number of steps a single loop takes and the number a nested loop takes. Print one line per size, in exactly this format: n=10 linear=10 quadratic=100

Expected output: n=10 linear=10 quadratic=100
n=100 linear=100 quadratic=10000

Show AP pseudocode reference
FOR EACH n IN [10, 100]
{
  linear ← n
  quadratic ← n * n
  DISPLAY("n=" + n + " linear=" + linear + " quadratic=" + quadratic)
}
You do not need to actually run the loops to count their steps: a single loop over n items takes n steps and a nested pair takes n times n. Look at what happens between the two lines. The input got ten times bigger and the linear count did too, but the quadratic count got a hundred times bigger.

Problem 4 of 4

Write from scratch. Some algorithms have to try every possible subset of their input, which takes 2 to the power of n steps. For n = 20, print the step count for a quadratic algorithm and for a subset algorithm, then print which of the two is unreasonable. Use exactly this format:
quadratic 400
subsets 1048576
subsets is unreasonable

Expected output: quadratic 400
subsets 1048576
subsets is unreasonable

Show AP pseudocode reference
n ← 20
quadratic ← n * n
subsets ← 2 ^ n
DISPLAY("quadratic " + quadratic)
DISPLAY("subsets " + subsets)
DISPLAY("subsets is unreasonable")
In Python 2 to the power of n is 2 ** n; in JavaScript it is 2 ** n as well. The gap is the whole point: at n = 20 the quadratic algorithm does 400 steps and the subset one does over a million. Push n to 60 and the subset algorithm outlasts a human lifetime, which is what "unreasonable time" means.

Your code runs on a secure external service. Answers are checked automatically and nothing is stored.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]