Skip to content
dsa/roadmap
2.1 ~30 min easy

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

PropertyMeaning
FinitenessIt must stop after a finite number of steps.
DefinitenessEvery step is clear and has exactly one meaning.
SequenceSteps run in a defined order; each step has a unique next step.
FeasibilityEvery step can actually be carried out with the available resources.
CorrectnessIt produces the right output for every valid input.
Language independenceThe steps can be written in any language (C++, Python, pseudocode).
CompletenessIt solves the whole problem, including edge cases.
EffectivenessEach step is basic enough to do exactly, in finite time, even by hand.
EfficiencyIt uses as little time and memory as possible.
GeneralityIt works for a whole class of problems, not one fixed input.
Input / OutputZero 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.

Loading visualizer…

04C++ code

Compiled and run with g++ -std=c++17; the output below is real. Type it out yourself before you copy it.

find-max.cpp19 lines · C++17
#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";
}
Expected output
max of a = 31
max of b = -2

05Complexity

findMax: comparisonsexactly n − 1
findMax: timeO(n), and best = worst
Extra spaceO(1)

Efficiency is one of the 11 properties. Chapter 2 is about measuring it precisely.

06Watch

Introduction to Algorithms

Abdul Bari · 11:48

Why this one: Abdul Bari covers what an algorithm is, its characteristics and how it differs from a program.

07Exam lens

Likely questions

  1. Define algorithm. List and explain any five properties of an algorithm.
  2. What is the difference between finiteness and effectiveness?
  3. Why must an algorithm be language independent?
  4. 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

  1. 1."Add a pinch of salt" breaks which property?
  2. 2.An algorithm must produce…
  3. 3.while (true) { } violates which property?
  4. 4."Works for any array size, not just 5 items" describes…

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.

0/4 ticked

View as Markdown · Chapter cheat sheet

Keyboard shortcuts

⌘K /
Search topics, glossary, cheat sheets
space
Play / pause the visualizer
← →
Step back / forward
r
Reset the visualizer
?
Show this help