Developing
Algorithms
Learn to design, express, and refine step-by-step procedures that solve computational problems: from everyday tasks to complex programs.
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.
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:
The Three Building Blocks
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:
a ← 10
// Selection
IF (\(a > 5\))
{
DISPLAY("Big")
}
ELSE
{
DISPLAY("Small")
}
// Iteration
REPEAT UNTIL (\(a = 0\))
{
a ← \(a - 1\)
}
How to Develop an Algorithm
Example: Find the Maximum
Problem: Given a list of numbers, find and display the largest value.
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.
{
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:
{
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.
Linear vs Binary Search
Common Mistakes
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
Consider the following pseudocode:
x ← 1
REPEAT UNTIL (\(x > 10\))
{
x ← \(x + 3\)
}
DISPLAY(x)
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
Consider the following pseudocode:
score ← 75
IF (\(\text{score} \ge 90\))
{
result ← "A"
}
IF (\(\text{score} \ge 80\))
{
result ← "B"
}
IF (\(\text{score} \ge 70\))
{
result ← "C"
}
DISPLAY(result)
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?