01 / 21

Developing
Algorithms

Learn to design, express, and refine step-by-step procedures that solve computational problems: from everyday tasks to complex programs.

AP Computer Science Principles

What Is an Algorithm?

An algorithm is a finite set of instructions that accomplishes a specific task. Every algorithm must be clear, ordered, and produce a result in a finite number of steps.

Sequencing
Steps execute one after another in a defined order.
Selection
Decisions (IF/ELSE) choose which path to follow.
Iteration
Loops repeat steps until a condition is met.

Why Algorithms Matter

Algorithms are the foundation of all software. Before you write a single line of code, you need a clear plan: an algorithm. They let us reason about correctness and efficiency independently of any programming language.

Expressing Algorithms

Algorithms can be expressed in multiple representations. Each serves a different audience and purpose:

Natural Language
Plain English descriptions: easy to read, but can be ambiguous.
Pseudocode
Structured text that blends code-like syntax with human readability.
Flowcharts
Visual diagrams using shapes and arrows to show process flow.

The Three Building Blocks

SEQUENCING Step A Step B Step C SELECTION Condition? True False Do X Do Y ITERATION Done? Yes Exit No Loop Body Steps run in order, top to bottom. A condition picks which path to take. Repeat until the condition is met.

Key Distinctions

Sequencing Selection Iteration
What it does Executes steps in order Chooses a path Repeats steps
Keywords Step 1, Step 2... IF, ELSE REPEAT, WHILE, FOR
When to use Always: forms the backbone When behavior depends on input When steps must repeat
Example Assign, then display \( \text{Grade} > 60 \to \text{pass} \) Sum all items in a list

Pseudocode Conventions

The AP CSP exam uses a specific pseudocode style. You need to recognize its notation:

// Assignment
a ← 10

// Selection
IF (\(a > 5\))
{
  DISPLAY("Big")
}
ELSE
{
  DISPLAY("Small")
}

// Iteration
REPEAT UNTIL (\(a = 0\))
{
  a ← \(a - 1\)
}

How to Develop an Algorithm

1
Define the problem. What are the inputs? What output do you need?
2
Identify the steps. Break the task into smaller, concrete actions.
3
Determine control structures. Decide where you need selection (IF) or iteration (REPEAT).
4
Write it out. Express the algorithm in pseudocode or a flowchart.
5
Trace and test. Walk through with sample inputs to verify correctness.

Example: Find the Maximum

Problem: Given a list of numbers, find and display the largest value.

// Input: list of numbers
max ← list[1]

FOR EACH item IN list
{
  IF (\(\text{item} > \text{max}\))
  {
    max ← item
  }
}

DISPLAY(max)

Tracing the Algorithm

Let's trace through with list = [3, 7, 2, 9, 4]:

Step item \( \text{item} > \text{max} \)? max
Init - - 3
1 3 False 3
2 7 True 7
3 2 False 7
4 9 True 9
5 4 False 9

Example: Nested Selection

Problem: Assign a letter grade based on a numeric score.

IF (\(\text{score} \ge 90\))
{
  grade ← "A"
}
ELSE
{
  IF (\(\text{score} \ge 80\))
  {
    grade ← "B"
  }
  ELSE
  {
    IF (\(\text{score} \ge 70\))
    {
      grade ← "C"
    }
    ELSE
    {
      grade ← "F"
    }
  }
}

Combining Existing Algorithms

You can build new algorithms by combining known ones. Suppose you already have findMax and findMin. You can create findRange:

PROCEDURE findRange(list)
{
  largest ← findMax(list)
  smallest ← findMin(list)
  RETURN (\(\text{largest} - \text{smallest}\))
}

Algorithm Efficiency

Two correct algorithms can solve the same problem but differ dramatically in how many steps they require. Efficiency matters as data grows.

Input Size (n) Steps Linear Quadratic

Linear vs Binary Search

Linear Search
Check each element one by one from the start. Works on any list. Worst case: \( n \) steps.
Binary Search
Halve the search space each step. Requires a sorted list. Worst case: O(\( \log n \)) steps.
2 5 8 12 15 22 31 40 middle eliminated

Common Mistakes

✕ Off-by-One Errors
Starting a loop at index 0 vs 1, or using \( < \) vs \( \le \). Always trace boundary values.
✕ Infinite Loops
Forgetting to update the loop variable so the termination condition is never reached.
✕ Wrong Condition Order
In nested IF/ELSE, checking a less restrictive condition first catches inputs you didn't intend.

Summary of Key Concepts

Concept What It Means AP Exam Tip
Sequencing Steps execute in order Trace line by line
Selection IF/ELSE branching Check condition order
Iteration REPEAT / FOR EACH loops Watch boundary values
Combining Reusing existing procedures Know procedure calls
Efficiency How steps grow with input Linear vs binary search

Question 1 of 5

Which of the following is NOT one of the three fundamental building blocks of algorithms?

Question 2 of 5

What value is displayed when this code runs?

Question 3 of 5

A binary search is performed on a sorted list of 128 elements. What is the maximum number of comparisons needed to find an element?

Question 4 of 5

What is displayed? Notice the code uses separate IF statements, not IF/ELSE.

Question 5 of 5

Which situation requires the use of iteration (a loop) rather than just sequencing and selection?