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.
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 ← 0before a loop withx ← x + …→ sum or count.found ← falsebefore 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 + 1counts.sum ← sum + itemsums. 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.
vals ← [4, 9, 2, 7]?
count ← 0
FOR EACH v IN vals
{
IF (v > 5)
{
count ← count + 1
}
}
DISPLAY(count)Show answer
Answer: A. This is the counting pattern. Values greater than 5 are 9 and 7 → count = 2.
best ← list[1]
FOR EACH x IN list
{
IF (x > best)
{
best ← x
}
}Which single change accomplishes this?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