Every step must be unambiguous
"Choose a good pivot" is not a step. If two people executing the description could do different things, it is not an algorithm.
An algorithm is a finite sequence of precise steps producing an output from an input. Pseudocode is how it is communicated: precise enough to analyse, free of the details of any language.
Core ideas
The definition rules out things that look like algorithms and are not, and each requirement rules out a specific failure.
"Choose a good pivot" is not a step. If two people executing the description could do different things, it is not an algorithm.
On every valid input. A procedure that may run forever is not an algorithm, however useful it is.
Detailed enough to analyse and implement, free of syntax. Analysis is about the method, not the language.
The material
The five requirements, pseudocode conventions, and one problem solved three ways.
A finite sequence of precise instructions transforming an input into an output. It must have: input from a specified set; definiteness, every step unambiguous; finiteness, terminating after finitely many steps; output in a specified relation to the input; and effectiveness, every step performable exactly.
Each requirement excludes something real. Definiteness excludes "pick a suitable value"; finiteness excludes a loop that may never exit; effectiveness excludes "decide whether this program halts", which “Decidability and the Halting Problem” shows is not performable at all.
The conventions used on this course: indentation for blocks, = for assignment, ← avoided, one-based indexing, and enough English to keep it readable. Nothing depends on a language, and the analysis of “Analysing Iterative Algorithms” counts the comparisons regardless of how it is implemented.
Computing xⁿ for a positive integer n:
All three are correct and they differ enormously in cost. At n = 1000, A performs 999 multiplications and B performs about 15. That gap is the subject of this course, and it is a property of the algorithm rather than of the machine.
An algorithm that is fast and wrong is worthless, and the order of work is therefore fixed: establish correctness (“Correctness and Termination”), then measure cost (growth rates, asymptotic bounds, iterative analysis, searching and sorting, divide and conquer, recursion trees and amortised analysis), then look for a better method (“Greedy Algorithms” and “Dynamic Programming”).
The order is worth stating because the temptation runs the other way. Optimising an algorithm before knowing it is right produces a faster wrong answer, and the speed makes the error harder to find.
Interactive
Counts from running, not from quoting
Method
State the contract before the steps.
What is given, what is produced, and the relation between them. This is the specification, and “Correctness and Termination” proves the algorithm meets it.
No step may require judgement. If it does, it is a subproblem needing its own algorithm.
Exhibit a quantity that strictly decreases and is bounded below, as in “Recursive Algorithms and Correctness” in Induction and Recursion.
Count the dominant operation as a function of the input size. That is “Analysing Iterative Algorithms”.
The specification is the part usually skipped.
The specification says "an index", not "the first index". If the caller needs the first occurrence that must be stated, because it constrains which algorithms are acceptable — binary search on a sorted list with duplicates does not guarantee it without modification.
Specify and write an algorithm that returns the two largest values in a list. State what happens when the list has fewer than two elements — the edge case the specification exists to settle.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.