1. Big Idea: Algorithms Give Objects and Data a Purpose
Units 1–5 taught students how to store information, make decisions, repeat actions, design objects, and organize collections. Unit 6 asks the next question: How do we use those tools to solve problems efficiently and flexibly?
Algorithms
Step-by-step strategies for finding, sorting, ranking, and processing data.
linear searchbinary searchselection sortinsertion sortmerge sortInheritance
A way to model relationships between classes, such as a general Athlete and more specific Runner, Jumper, or Thrower.
Recursion
A method-solving pattern where a method calls itself on a smaller version of the problem until it reaches a base case.
base caserecursive callstack2. Searching Algorithms
Linear Search: Stack of Papers
Linear search checks values one at a time. It works on unsorted or sorted data, but it may need to inspect every element.
public static int linearSearch(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
return i;
}
}
return -1;
}Why return -1?
Indexes are never negative, so -1 is a useful signal that the target was not found.
Binary Search: Dictionary Strategy
Binary search repeatedly checks the middle of a sorted collection and throws away half of the remaining search space.
public static int binarySearch(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (nums[mid] == target) {
return mid;
} else if (target < nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}| Algorithm | Data Requirement | Best Use | Informal Efficiency |
|---|---|---|---|
| Linear Search | No sorting required | Small or unsorted lists | May check every item |
| Binary Search | Must be sorted | Large sorted lists | Cuts the problem in half repeatedly |
3. Sorting Algorithms
Sorting puts data into an order. AP CSA expects students to recognize, trace, and reason about standard sorts.
Selection Sort: Pick the Fastest Athlete
Find the smallest remaining value and swap it into the next open position.
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}Insertion Sort: Cards in Your Hand
Take the next item and slide it left until it fits into the sorted portion.
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}Merge Sort: Merge Two Sorted Race Results
Merge sort breaks a list into smaller lists, sorts them, then merges sorted pieces back together.
// AP students should understand the recursive idea more than memorize full code:
// 1. Split the array into halves.
// 2. Recursively sort each half.
// 3. Merge the two sorted halves.Why are tracing tables useful for sorting?
Sorting algorithms change the collection step by step. A tracing table prevents students from losing track of indexes, swaps, and shifted values.
4. Inheritance: Class Family Trees
Inheritance models an is-a relationship. A Runner is an Athlete. A Jumper is an Athlete. Shared information belongs in the superclass.
public class Athlete {
private String name;
private int grade;
public Athlete(String name, int grade) {
this.name = name;
this.grade = grade;
}
public String getName() {
return name;
}
public int getGrade() {
return grade;
}
public String toString() {
return name + " (grade " + grade + ")";
}
}public class Runner extends Athlete {
private double bestMile;
public Runner(String name, int grade, double bestMile) {
super(name, grade);
this.bestMile = bestMile;
}
public double getBestMile() {
return bestMile;
}
public String toString() {
return super.toString() + ", mile PR: " + bestMile;
}
}| Keyword | Meaning | Common AP Use |
|---|---|---|
extends | Creates a subclass relationship | Runner extends Athlete |
super(...) | Calls the superclass constructor | Initialize inherited fields |
super.method() | Calls a superclass method | Reuse part of toString() |
| overriding | Subclass replaces inherited behavior | Subclass-specific toString() |
5. Polymorphism: One Clipboard, Different Athletes
Polymorphism means a superclass reference can point to a subclass object. The reference type controls what methods are visible at compile time, but the actual object controls overridden behavior at run time.
Athlete a = new Runner("Mia", 12, 5.12);
System.out.println(a.toString());Even though the variable type is Athlete, Java runs the Runner version of toString() because the actual object is a Runner.
Runner has a method that Athlete does not have, you cannot call it through an Athlete reference unless you cast.// a.getBestMile(); // compile error if a is declared as AthleteWhy is polymorphism useful?
A program can store different kinds of athletes in one Athlete[] or ArrayList<Athlete>, then rely on overridden methods to produce behavior specific to each subclass.
6. Interfaces and Comparable
An interface is a contract. A class that implements an interface promises to provide certain methods.
For AP CSA, the most useful interface to preview is Comparable, which allows objects to define how they should be ordered.
public class Runner extends Athlete implements Comparable<Runner> {
private double bestMile;
public int compareTo(Runner other) {
if (bestMile < other.bestMile) {
return -1;
} else if (bestMile > other.bestMile) {
return 1;
} else {
return 0;
}
}
}compareTo is the rule sheet for ranking. For runners, smaller time is better. For throws, larger distance is better.7. Recursion: A Method Calling Itself
Recursion solves a problem by reducing it to a smaller version of the same problem.
Base Case
The stopping point. Without it, recursion never ends.
Recursive Call
The method calls itself with a smaller or simpler input.
Progress Toward Base Case
Each call must move closer to stopping.
Countdown Example
public static void countdown(int n) {
if (n == 0) {
System.out.println("Go!");
} else {
System.out.println(n);
countdown(n - 1);
}
}Trace countdown(3)
countdown(3) prints 3, calls countdown(2)
countdown(2) prints 2, calls countdown(1)
countdown(1) prints 1, calls countdown(0)
countdown(0) prints Go!Factorial Example
public static int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}Recursive Binary Search
public static int binarySearch(int[] nums, int target, int low, int high) {
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (nums[mid] == target) {
return mid;
} else if (target < nums[mid]) {
return binarySearch(nums, target, low, mid - 1);
} else {
return binarySearch(nums, target, mid + 1, high);
}
}8. Try It in Java
Use the IDE to run the examples. A good student task is to change the data, add print statements, and trace the result before running.
Starter Program: Athlete Ranking System
import java.util.ArrayList;
class Athlete {
private String name;
private int grade;
public Athlete(String name, int grade) {
this.name = name;
this.grade = grade;
}
public String getName() { return name; }
public int getGrade() { return grade; }
public String toString() {
return name + " grade " + grade;
}
}
class Runner extends Athlete {
private double mileTime;
public Runner(String name, int grade, double mileTime) {
super(name, grade);
this.mileTime = mileTime;
}
public double getMileTime() { return mileTime; }
public String toString() {
return super.toString() + " mile: " + mileTime;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Runner> team = new ArrayList<Runner>();
team.add(new Runner("Mia", 12, 5.12));
team.add(new Runner("Alex", 11, 5.35));
team.add(new Runner("Sam", 10, 5.05));
Runner best = team.get(0);
for (Runner r : team) {
if (r.getMileTime() < best.getMileTime()) {
best = r;
}
}
System.out.println("Best runner: " + best);
}
}9. AP-Style Tracing Challenges
1. What does this print?
int[] a = {4, 2, 7, 1};
int max = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
System.out.println(max);Answer: 7. The algorithm tracks the largest value seen so far.
2. What does this print?
public static int mystery(int n) {
if (n == 0) return 0;
return n + mystery(n - 1);
}
System.out.println(mystery(4));Answer: 10, because 4 + 3 + 2 + 1 + 0 = 10.
3. Which toString runs?
Athlete a = new Runner("Mia", 12, 5.12);
System.out.println(a.toString());Answer: The Runner version, because overridden methods are chosen based on the actual object type at run time.
4. Why can binary search fail here?
int[] nums = {9, 1, 4, 7, 2};
binarySearch(nums, 7);Answer: The array is not sorted. Binary search requires sorted data.
10. Debugging Challenges
Bug 1: Infinite recursion
public static int factorial(int n) {
return n * factorial(n);
}Problem: The argument never gets smaller and there is no base case. It should stop at n == 0 or n == 1 and call factorial(n - 1).
Bug 2: Binary search on unsorted data
Problem: Binary search is only valid when the data is sorted in the same order the algorithm assumes.
Bug 3: Subclass constructor does not call superclass constructor
public Runner(String name, int grade, double mileTime) {
this.mileTime = mileTime;
}Problem: If the superclass does not have a no-argument constructor, the subclass must call super(name, grade).
Bug 4: Calling subclass-only method through superclass reference
Athlete a = new Runner("Mia", 12, 5.12);
System.out.println(a.getMileTime());Problem: The variable type is Athlete. Java only allows methods declared in Athlete unless the reference is cast.
11. AP-Style Multiple Choice
1. Which algorithm requires sorted data?
Answer: Binary search.
2. What keyword creates a subclass?
Answer: extends.
3. What is required in every correct recursive method?
Answer: A base case and progress toward that base case.
4. In selection sort, what usually happens during each outer-loop pass?
Answer: The algorithm finds the smallest remaining value and swaps it into its final position.
5. What method does Comparable require?
Answer: compareTo.
12. FRQ Practice
FRQ A: Search and Rank Runners
Assume a Runner class has getName() and getMileTime(). Write a method bestRunner that takes an ArrayList<Runner> and returns the runner with the lowest mile time. If the list is empty, return null.
Show solution
public static Runner bestRunner(ArrayList<Runner> team) {
if (team.size() == 0) {
return null;
}
Runner best = team.get(0);
for (Runner r : team) {
if (r.getMileTime() < best.getMileTime()) {
best = r;
}
}
return best;
}FRQ B: Recursive Sum
Write a recursive method sumTo that returns the sum of all integers from 1 to n. For example, sumTo(4) returns 10.
Show solution
public static int sumTo(int n) {
if (n == 1) {
return 1;
}
return n + sumTo(n - 1);
}Extension: add input validation for values less than 1.
FRQ C: Inheritance Design
Design a superclass EventPerformance and a subclass RunningPerformance. The superclass should store athlete name and event name. The subclass should add time in seconds and override toString().
Rubric focus
- Superclass private instance variables and constructor.
- Subclass uses
extends. - Subclass constructor calls
super. - Subclass adds its own private data.
toString()uses inherited information and subclass information.
13. AP Exam Strategy: Recognize the Problem Type
| Prompt Clue | Likely Skill | How to Start |
|---|---|---|
| "find", "contains", "index of" | Search | Loop through data; return when target is found. |
| "rank", "order", "least to greatest" | Sort or compare | Choose comparison rule and trace swaps or shifts. |
| "extends", "super", overridden method | Inheritance | Identify superclass fields and subclass-specific behavior. |
| "method calls itself" | Recursion | Find base case, recursive call, and shrinking input. |
| "ArrayList of objects" | Object collection algorithm | Use size(), get(), and object methods. |
14. Capstone Project: Track Meet Ranking System
This project ties together the entire AP CSA site. Students build a program that ranks athletes or physics simulation results using inheritance and algorithms.
Required Features
- Superclass:
AthleteorPerformance - At least two subclasses, such as
RunnerandFieldEventAthlete ArrayListstorage- Linear search by name or event
- Sorting or ranking algorithm written by the student
- At least one overridden
toString() - At least one recursive method or recursive search extension
- Testing table with expected and actual output
Project Options
Track Meet Ranking
Rank runners by time and field athletes by distance.
Projectile Simulation Ranking
Rank launch angles by range and time of flight.
Spring-Mass Trial Analyzer
Rank trials by period, amplitude, or energy estimate.
Orbit Stability Analyzer
Rank simulated orbits by stability or closest approach.
Suggested grading priorities
Grade strongest on algorithm correctness, meaningful class design, clear inheritance relationships, working search/rank features, and student explanation of how the model makes decisions.