From the free course AP Computer Science A on Lykke
1.1 Introduction to Algorithms, Programming, and Compilers
Key concepts: Algorithms as step-by-step procedures for solving problems · Program design and expressing solutions precisely in Java · Testing programs and identifying and correcting errors · Comparing alternative solutions to the same problem · Compilers and the translation of Java source code · Sorting algorithms · Kinesthetic learning through physically arranging students in sorted order · Collaborative and individual problem-solving in laboratory activities
An algorithm is a finite, ordered set of instructions that accomplishes a specific task. A recipe, a route-finding procedure, and a Java program can all be algorithms when their steps are precise enough to follow and eventually stop.
1.1 Introduction to Algorithms, Programming, and Compilers
An algorithm is a finite, ordered set of instructions that accomplishes a specific task. A recipe, a route-finding procedure, and a Java program can all be algorithms when their steps are precise enough to follow and eventually stop.
1.1.A.1 — An algorithm is a finite set of instructions that accomplishes a specific task.
Suppose a school needs to identify the largest value in a list of temperatures. One reliable procedure is:
- Treat the first temperature as the current largest.
- Examine each remaining temperature.
- If a temperature is larger than the current largest, replace the current largest.
- Report the final largest value.
The procedure works because it gives an initial condition, a repeated action, a decision, and a final result. Its instructions are not tied to one particular list; the same algorithm can process any list that meets its assumptions.
A program is an algorithm expressed in a programming language so that a computer can execute it. Java provides precise syntax and vocabulary for expressing instructions, while the algorithm supplies the underlying plan.
1.1.A.2 — Algorithms are implemented using programming languages.
For example, the largest-value procedure can be written as a Java method:
public static int largest(int[] values) {
int largest = values[0];
for (int index = 1; index < values.length; index++) {
if (values[index] > largest) {
largest = values[index];
}
}
return largest;
}
The code is precise: it specifies where to begin, which elements to examine, what comparison to perform, and what value to return. A vague instruction such as “look for the biggest temperature” may communicate an intention to a person, but it does not contain enough operational detail for a computer.
From Java source code to execution
The Java statements that a programmer writes are called source code. A compiler translates source code written in a high-level programming language into a lower-level form that the computer’s execution environment can use.
1.1.A.3 — A compiler translates source code written in a high-level programming language into machine code.
The translation pipeline can be pictured as:
Java source code
|
v
compiler checks syntax and translates
|
v
translated program representation
|
v
execution by the computer
A compiler can detect syntax errors, such as a missing parenthesis or semicolon. Compilation, however, does not prove that the algorithm is correct. A program may compile successfully and still produce the wrong result because its instructions implement a flawed plan.
Testing, debugging, and comparing solutions
Testing means running a program with selected inputs and checking whether the outputs match what should happen. Debugging means locating and correcting errors. Strong testing includes ordinary cases, boundary cases, and cases designed to expose a mistaken assumption.
For largest, useful tests include:
| Test input | Expected result | What it checks |
|---|---|---|
{4, 9, 2} |
$9$ | A typical case |
{-8, -3, -12} |
$-3$ | Whether negative values are handled |
{7} |
$7$ | The smallest permitted input |
{5, 5, 5} |
$5$ | Ties and repeated values |
Named misconception — “If it compiles, it works.” Compilation checks whether the source code follows the language’s rules; testing checks whether the running program behaves as intended. These are different questions.
Two algorithms can solve the same problem while differing in clarity, number of steps, or behavior on special inputs. For example, a group might compare a procedure that repeatedly selects the next largest value with a procedure that scans once while maintaining a current maximum. The second procedure avoids unnecessary rearrangement and is often easier to justify for finding only one largest value.
Laboratory investigation: people as a sorting algorithm
A physical activity makes sorting visible. Gather 10–15 students and give each student a card containing a distinct height, number, or fictional identification value.
First, ask the group to arrange themselves from smallest value to largest value without prescribing a method. Then run the activity again using a stated procedure—for example, repeatedly find the shortest remaining person and move that person to the next open position. Count comparisons, note movements, and record moments when two people are unsure what “next” means.
Next, have teams invent and explain a different sorting procedure. Teams should test their procedures on an already sorted arrangement, a reverse-sorted arrangement, and an arrangement with repeated values. When an arrangement is wrong, identify the exact decision or step that caused the error rather than merely restarting.
After the collaborative investigation, each student should individually design a procedure for a related task, such as finding the two largest values or inserting one new value into an ordered line. Each student must write the steps precisely, trace them on a small example, and justify why the procedure terminates with the required result.
This laboratory cycle develops Skill 1.A: Determine an appropriate program design to solve a problem or accomplish a task. It also makes the central workflow concrete: design a solution, express it precisely in Java, test it, correct errors, and compare it with alternatives.
Retrieval check
A program compiles and produces the wrong maximum only when the input is in reverse order. Is the primary problem a compilation error or a logic error? What additional test would help locate it?
Answer: It is a logic error because the program executes but violates the intended behavior. Test a typical unordered list, a one-element list, and an all-negative list; tracing the current-largest value after each comparison can reveal where the algorithm’s assumption fails.


Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A
- About the Advanced Placement Program ® (AP ®)
- AP Computer Science A-AP Students-College Board
- AP Computer Science A Textbook-Code HS
- AP Computer Science A - Code.org