Algorithms & Their Properties
An algorithm is a finite, unambiguous sequence of steps that solves a problem. Learn the 11 properties every correct algorithm must have.
Rule of this site: Don't just watch: implement it yourself, then solve 2–3 problems.
02Mental model
An algorithm is a recipe precise enough for a machine: a finite sequence of well-defined steps that takes some input and produces the correct output.
A cooking recipe that says “bake until it looks good” is fine for humans, but it isn’t an algorithm. A computer can’t judge “looks good”. Every step must be exact.
The 11 properties
| Property | Meaning |
|---|---|
| Finiteness | It must stop after a finite number of steps. |
| Definiteness | Every step is clear and has exactly one meaning. |
| Sequence | Steps run in a defined order; each step has a unique next step. |
| Feasibility | Every step can actually be carried out with the available resources. |
| Correctness | It produces the right output for every valid input. |
| Language independence | The steps can be written in any language (C++, Python, pseudocode). |
| Completeness | It solves the whole problem, including edge cases. |
| Effectiveness | Each step is basic enough to do exactly, in finite time, even by hand. |
| Efficiency | It uses as little time and memory as possible. |
| Generality | It works for a whole class of problems, not one fixed input. |
| Input / Output | Zero or more inputs; at least one output. |
Memory trick: “Fine Dogs Sit, Feel Calm, Learn Commands; Every Excited Good I/O.” Silly, but it sticks.
Algorithm vs program
A program is an algorithm written in a specific language and run on a machine. A program may run forever (a web server); an algorithm must finish.
03Visualize
space play · ←→ step · r reset. The highlighted code line follows each step.
04C++ code
Compiled and run with g++ -std=c++17; the output below is real. Type it out yourself before you copy it.
#include <iostream>
using namespace std;
// Input: an array and its size. Output: the largest value.
// Finite (n-1 comparisons), definite, effective, general, correct.
int findMax(const int a[], int n) {
int max = a[0];
for (int i = 1; i < n; i++)
if (a[i] > max)
max = a[i];
return max;
}
int main() {
int a[] = {14, 3, 27, 9, 31, 8};
int b[] = {-5, -2, -9};
cout << "max of a = " << findMax(a, 6) << "\n";
cout << "max of b = " << findMax(b, 3) << "\n";
}max of a = 31 max of b = -2
05Complexity
| findMax: comparisons | exactly n − 1 |
|---|---|
| findMax: time | O(n), and best = worst |
| Extra space | O(1) |
Efficiency is one of the 11 properties. Chapter 2 is about measuring it precisely.
06Watch
Introduction to Algorithms
Why this one: Abdul Bari covers what an algorithm is, its characteristics and how it differs from a program.
07Exam lens
Likely questions
- Define algorithm. List and explain any five properties of an algorithm.
- What is the difference between finiteness and effectiveness?
- Why must an algorithm be language independent?
- Give an example of a set of steps that is NOT an algorithm and say which property it breaks.
Common mistakes
- Confusing definiteness (each step has one clear meaning) with finiteness (the algorithm stops).
- Saying an algorithm needs at least one input. It needs zero or more inputs but at least one output.
- Treating "a program" and "an algorithm" as the same. A program may run forever (an OS); an algorithm must terminate.
08Quick check
09Practice
🟢 Trace by hand
Here is a "recipe": (1) pick a random number, (2) if it is the answer, stop; otherwise go to (1). Which properties does it break?
Show answer
- Finiteness: it may never stop.
- Definiteness: "pick a random number" is not one exact action.
- Efficiency: there is no bound on the number of steps.
🟡 Implement it
Write findMin and a main() that tests it on an array of negative numbers. Then write which property your test checks.
Hint
Start with min = a[0], not min = 0. With 0 the algorithm is incorrect for all-positive arrays. That's the correctness property.
🔴 Solve problems
Hint
Find min and max in one pass, then apply Euclid's algorithm (a classic finite, definite algorithm).
Solution idea
int g(int a, int b){ return b == 0 ? a : g(b, a % b); }applied to min and max.Hint
A running sum and a running max. Write the steps in plain English first, then code.
10Mastery checklist
Tick these honestly. When every box is ticked, the topic is marked complete.