COS 102 · Problem Solving
Our notes
2 min readDefinition
An algorithm is a finite, ordered set of clear instructions that solves a problem, taking inputs and producing outputs. The word comes from the ninth-century Persian mathematician al-Khwarizmi.
Properties of a good algorithm
- Finiteness: it stops after a finite number of steps.
- Definiteness: each step is clear and has only one meaning.
- Input: zero or more values are given to it.
- Output: at least one result is produced.
- Effectiveness: every step can actually be done (by hand, in principle).
- Correctness: it gives the right answer for every valid input.
- Generality: it solves a whole class of problems, not one case.
"Stir until it looks right" fails definiteness; "keep adding 1 until you reach the largest number" fails finiteness.
Example: the largest of a list
- Set LARGEST to the first number.
- For each remaining number: if it is greater than LARGEST, set LARGEST to it.
- Output LARGEST.
It has inputs, an output, clear steps, and stops after the last number.
Example: Euclid's algorithm for the HCF
To find the highest common factor of two whole numbers a and b:
- If b is 0, the answer is a. Stop.
- Otherwise, replace (a, b) by (b, remainder of a ÷ b) and go to step 1.
Worked example
HCF of 84 and 36:
- (84, 36): 84 = 2 × 36 + 12 → (36, 12)
- (36, 12): 36 = 3 × 12 + 0 → (12, 0)
- b = 0, so HCF = 12.
Check: 84 = 12 × 7 and 36 = 12 × 3, and 7 and 3 share no factor ✓.
Comparing algorithms (efficiency)
Two correct algorithms can differ greatly in speed. To find a name in a sorted list of 1,000 names:
- Linear search checks one by one: up to 1,000 checks.
- Binary search checks the middle, then throws away half each time: at most 10 checks (2¹⁰ = 1,024).
We describe this growth with big-O notation: linear search is O(n), binary search is O(log n). Efficiency matters more as data grows.
Exams usually ask
- State and explain the properties of an algorithm.
- Write an algorithm for a simple task and trace it with given data.
Common mistake
Writing steps that depend on judgement ("pick a good number"). A computer needs an exact rule.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick Introduction to Computer Science, 3.3 Formal Properties of Algorithms
(opens openstax.org in a new tab)
OpenStax, Introduction to Computer Science, 3.3 Formal Properties of Algorithms, CC BY-NC-SA 4.0 · licence
-
Algorithm
(opens en.wikipedia.org in a new tab)
Wikipedia, Algorithm, CC BY-SA 4.0 · licence
-
Euclidean algorithm
(opens en.wikipedia.org in a new tab)
Wikipedia, Euclidean algorithm, CC BY-SA 4.0 · licence
-
OpenStax, Introduction to Computer Science (whole free textbook)
The whole book, free to read online or download as a PDF (the PDF is large: Wi-Fi).
(opens openstax.org in a new tab)
OpenStax, OpenStax, Introduction to Computer Science (whole free textbook), CC BY-NC-SA 4.0 · licence
-
NPTEL: Problem Solving through Programming in C (IIT Kharagpur, whole course)
Algorithms, flowcharts and pseudocode, then variables, decisions, loops, arrays and functions in C (the ideas carry over to Fortran and Python). Video lectures, many hours in all: watch on Wi-Fi where you can.
(opens nptel.ac.in in a new tab)
NPTEL, NPTEL: Problem Solving through Programming in C (IIT Kharagpur, whole course), CC BY-NC-SA 4.0 · licence
Watch
-
What's an algorithm? - David J. Malan
5 min · ≈ 23 MB · about ₦13
(opens youtube.com in a new tab)
-
Lec 2: What is Algorithm and Need of Algorithm | Properties of Algorithm | Algorithm vs Program
8 min · ≈ 36 MB · about ₦20
(opens youtube.com in a new tab)
-
Characteristics of an Algorithm
15 min · ≈ 68 MB · about ₦38
(opens youtube.com in a new tab)
-
Algorithms Explained: Computational Complexity
21 min · ≈ 95 MB · about ₦53
(opens youtube.com in a new tab)
-
Discrete Math 3.1.1 Algorithms and Their Properties
11 min · ≈ 50 MB · about ₦28
(opens youtube.com in a new tab)
-
1. Algorithms and Computation
46 min · ≈ 207 MB · about ₦116
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.