The Stacks
BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.9

3.9 Developing Algorithms

An algorithm is a finite set of instructions that solves a problem. This topic is about how the three building blocks combine and the standard patterns you're expected to recognize on sight.

What you need to know

  • An algorithm is a finite sequence of precise instructions that accomplishes a task. It must end.
  • Every algorithm is built from sequencing, selection, and iteration. The exam asks you to identify which one a code fragment illustrates.
  • Algorithms can be expressed in natural language, flowcharts, pseudocode, or code — the same algorithm in different forms is still the same algorithm.
  • Different algorithms can solve the same problem; they may differ in efficiency or clarity but produce the same result.
  • Standard patterns to recognize: finding the max/min (track the best so far), computing a sum or average (accumulator), counting items that meet a condition, checking whether a value is present in a list, and building a new list from filtered elements.
  • Existing algorithms can be combined or modified to solve new problems — you don't start from scratch every time.
  • Reading an algorithm: identify what's initialized, what changes each iteration, and what's true when it stops. That reveals its purpose.

Worked example

Find the largest value in a list — the canonical pattern:

biggest ← nums[1]
FOR EACH n IN nums
{
    IF (n > biggest)
    {
        biggest ← n
    }
}
DISPLAY(biggest)

Initialize to the first element (never to 0 — that fails for all-negative lists), compare each element, keep the winner. Swap > for < and it finds the minimum. Replace the IF with total ← total + n and it sums. Recognizing these variants is faster than re-deriving them.

Trace it yourself

Step through with the buttons, or use the ← → keys. Changed variables are highlighted.

Exam tip: When asked "what does this algorithm do," don't trace with random values — trace with a tiny list of 3 items and watch what the tracked variable equals at the end. The initialization line usually tells you the pattern before you've read anything else.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • The CED's definition of an algorithm has three properties: it's a finite set of instructions (it ends), the instructions are precise (no ambiguity), and following them accomplishes a task. "Add salt to taste" fails precision; "count forever" fails finiteness.
  • Every algorithm can be built from the three control structures: sequencing, selection, and iteration. This is a theoretical result, and the exam treats it as a fact you should know. There's no fourth structure you need.
  • The same algorithm can be written in many forms — natural language, flowchart, pseudocode, any programming language — and it's still the same algorithm. Questions may show a flowchart and ask which pseudocode matches, or vice versa.
  • Different algorithms can solve the same problem. To find the maximum, you could scan once tracking the largest, or sort and take the last element. Both work; they differ in efficiency (3.17).
  • Recognizing a pattern from its initialization: x ← list[1] before a loop with a comparison → finding min or max. x ← 0 before a loop with x ← x + … → sum or count. found ← false before a loop → searching. Read the setup line first.
  • Modifying an existing algorithm — changing > to < to find min instead of max, or changing a count into a sum — is a named skill. The exam gives working code and asks what single change achieves a new goal.
  • The CED expects you to know these specific algorithms: finding max/min, computing sum/average, counting elements meeting a condition, determining whether a value is in a list, and combining or reordering lists. These appear as both "what does this do" and "which code does this" questions.

Mistakes that cost points

  • Initializing max to 0. Fails for lists of all negative numbers. Correct: initialize to the first element. The exam asks "for which list does this algorithm fail" — the answer is the all-negative list.
  • Tracing with a realistic list. Use three elements. It's enough to see the pattern and short enough not to make arithmetic mistakes.
  • Confusing count and sum. count ← count + 1 counts. sum ← sum + item sums. Look at what's being added.
  • Assuming a flowchart is a different algorithm. Form doesn't matter; logic does.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 What does the following code display, where vals ← [4, 9, 2, 7]?
count ← 0
FOR EACH v IN vals
{
    IF (v > 5)
    {
        count ← count + 1
    }
}
DISPLAY(count)
  1. A 2
  2. B 4
  3. C 16
  4. D 22
Show answer

Answer: A. This is the counting pattern. Values greater than 5 are 9 and 7 → count = 2.

Q2 A programmer wants to modify the algorithm below so it finds the smallest value instead of the largest.
best ← list[1]
FOR EACH x IN list
{
    IF (x > best)
    {
        best ← x
    }
}
Which single change accomplishes this?
  1. A Change best ← list[1] to best ← 0
  2. B Change x > best to x < best
  3. C Change best ← x to x ← best
  4. D Remove the IF statement
Show answer

Answer: B. Flipping the comparison makes it track the smallest value seen. Initializing to 0 would break the algorithm for lists of positive numbers.

Key vocabulary

Algorithm
a finite set of precise instructions that accomplishes a task
Sequencing
statements executed in order
Selection
choosing a path with a conditional
Iteration
repeating with a loop
Accumulator
a variable that builds up a result, such as a running total, across iterations