Efficiency of an algorithm (College Board AP® Computer Science Principles): Study Guide

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Measuring algorithm efficiency

How is algorithm efficiency measured?

  • A problem is a general description of a task that can (or cannot) be solved algorithmically

  • An instance of a problem includes a specific input; for example, sorting is a problem; sorting the list (2, 3, 1, 7) is an instance of that problem

  • Efficiency is an estimation of the amount of computational resources used by an algorithm, typically expressed as a function of the size of the input

  • An algorithm's efficiency is determined through formal or mathematical reasoning

  • An algorithm's efficiency can be informally measured by determining the number of times a statement or group of statements executes

  • Different correct algorithms for the same problem can have different efficiencies

  • Measuring efficiency helps programmers choose the best algorithm for a given task

Types of problems

  • A decision problem has a yes/no answer, for example, "Is there a path from A to B?"

  • An optimization problem aims to find the best solution among many options, for example, "What is the shortest path from A to B?"

Reasonable vs unreasonable time

How do we describe whether an algorithm runs quickly enough to be useful?

  • Algorithms with a polynomial efficiency or lower (constant, linear, square, cube, and so on) are said to run in a reasonable amount of time

  • Algorithms with exponential or factorial efficiencies run in an unreasonable amount of time, because the number of steps grows too quickly for the algorithm to be practical on large inputs

  • The distinction between reasonable and unreasonable time is about how efficiency scales with input size, not about whether an algorithm is fast on any single input

Problems that cannot be solved in a reasonable amount of time

  • Some problems cannot be solved in a reasonable amount of time, because there is no efficient algorithm for solving them

  • In these cases, approximate solutions are sought instead of exact ones

  • A heuristic is an approach to a problem that produces a solution that is not guaranteed to be optimal, but may be used when techniques that always find an optimal solution are impractical

  • For example, a heuristic might follow a simple rule that produces a good-enough solution quickly, instead of exhaustively checking every possibility

  • Heuristics trade guaranteed optimality for practicality

Examiner Tips and Tricks

  • When a question asks whether an algorithm runs in a reasonable amount of time, look for how the number of steps grows as the input size grows — not at the number of steps for any single input. Polynomial or lower = reasonable; exponential or factorial = unreasonable.

  • If a question describes a problem that cannot be solved in a reasonable amount of time and mentions accepting an approximate answer, the concept being tested is heuristics. The AP exam will not ask you to produce or identify a specific heuristic — only to recognize when a heuristic approach is appropriate.

  • For the AP Create Performance Task, if your program processes a list or dataset, you can discuss in your written response whether your chosen algorithm is efficient for the expected input size, and how its efficiency would change for much larger inputs.

Worked Example

A software team has written two different algorithms that both sort a list of names correctly. Algorithm X has polynomial efficiency; Algorithm Y has exponential efficiency.

For large inputs, which of the following is true?

(A) Algorithm Y runs faster because exponential growth is more efficient than polynomial growth

(B) Algorithm X runs in a reasonable amount of time; Algorithm Y does not

(C) Both algorithms run in a reasonable amount of time because they both solve the problem correctly

(D) Algorithm X runs in an unreasonable amount of time because polynomial growth is slower than exponential growth

[1]

Answer:

(B) Algorithm X runs in a reasonable amount of time; Algorithm Y does not [1 mark]

  • Algorithms with polynomial efficiency or lower run in a reasonable amount of time; algorithms with exponential or factorial efficiency run in an unreasonable amount of time. Correctness doesn't determine efficiency — both algorithms sort correctly, but Algorithm Y becomes impractical for large inputs because its step count grows too quickly.

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.