Recap
- An algorithm is a finite set of instructions that accomplish a specific task. It can be written as natural language, a diagram, pseudocode or code.
- Every algorithm is built from sequencing, selection and iteration.
- Algorithms are made from an idea, by combining known algorithms, or by modifying a known one.
- Standard algorithms to know: max/min of numbers, sum/average, divisibility (
n MOD d = 0), a robot path through a maze. - Reusing correct algorithms reduces development time and testing and simplifies finding errors.
- Algorithms can look alike and differ, or look different and agree.
Key relationships
| Task | Algorithm idea | Check |
|---|---|---|
| Largest of three | Start with a; replace if b is bigger; replace if c is bigger |
Test ties and first-is-largest |
| Smallest | Same, with < |
Change one operator |
| Range | largest − smallest | Combine two algorithms |
| Average | sum of values / count | Needs a sum first |
| Divisible | n MOD d = 0 |
Remainder zero |
| Conditional ↔ Boolean | IF(c) {r ← true} ELSE {r ← false} equals r ← c |
Equivalent for every input |
Assumptions
- Inputs are valid numbers unless stated.
- Variables that a segment never assigns keep their earlier values.
- Two algorithms are equivalent only if they give the same result for every input.
Method for comparing two algorithms
- List the inputs that matter: typical, boundary and equal/tie cases.
- Trace both algorithms on each input and record the results.
- If any input gives different results, the algorithms are not equivalent.
- If they always agree, explain why using the structure of the code, not just the tests.
Mistakes to avoid
- Calling algorithms equivalent after one or two tests.
- Forgetting the equal case in max/min code.
- Rewriting an algorithm from scratch when a modification would do.
- Describing an algorithm only as “it works” without listing the steps.
- Treating two IFs as an IF/ELSE.
Worked mini-example
x ← 4
y ← 4
big ← 0
IF(x > y)
{
big ← x
}
IF(y > x)
{
big ← y
}
DISPLAY(big)
Output: 0
Neither condition is true when x equals y, so big keeps its starting value 0, which is wrong. Replacing the second IF with an ELSE (or using ≥ in one of them) fixes it.
Quick self-check
- Name two standard algorithms you could combine to compute a range.
- Is
IF(c) {r ← true} ELSE {r ← false}equivalent tor ← c? - Give an input that separates
total > 100fromtotal ≥ 100.
Answers
- Find the maximum and find the minimum, then subtract.
- Yes: both store the value of
cinr. totalequal to 100.
Where marks are won and lost
Expect questions that show two segments and ask whether they produce the same result for all inputs, or for which input they differ. Others ask which existing algorithm helps to build a new one (for instance, an average needs a sum). Some ask you to express an algorithm in words or a diagram. When comparing, test the equal case and the boundaries first; these are where look-alikes diverge.
Back to the full study guide for worked examples and explanations.