An algorithm is a precise sequence of steps that solves a problem. This topic lays the foundation for the whole course: what an algorithm is, which properties it has, and why one problem can be solved by several algorithms.

What an algorithm is

An algorithm is a finite sequence of precisely described steps that turns input data into a result. Whoever follows the steps, the same input gives the same result. A recipe, a metro route, search and sorting are different examples of one idea.

Two algorithms for one problem
Two algorithms for one problem

Four properties

PropertyMeaning
Finitenessstops after a limited number of steps
Definitenessevery step is unambiguous
Input and outputzero or more inputs and at least one result
Effectivenesseach step is simple enough to do by hand

💡 A fifth property is often added: correctness, meaning the result satisfies the problem statement. We come back to it in topic 3.

The code below is an algorithm: the input is a list and target, the output is an index or -1, and the loop ends together with the list.

python
def find_index(items, target):
    for i, x in enumerate(items):
        if x == target:
            return i
    return -1


print(find_index([4, 8, 15, 16], 15))
print(find_index([4, 8, 15, 16], 23))
text
2
-1

Problem versus solution

A problem says what we need (the input and the expected output). An algorithm says how to get it. One problem can have many solutions. We compare them with three questions: is it correct, how fast is it, and how much memory does it need.

Example: the sum from 1 to n

The first algorithm adds the numbers one by one and makes n steps:

python
def sum_loop(n):
    total = 0
    for i in range(1, n + 1):
        total += i
    return total


print(sum_loop(100))
text
5050

The second uses the Gauss formula: 1 + 2 + ... + n = n(n + 1) / 2. That is a constant number of operations:

python
def sum_loop(n):
    total = 0
    for i in range(1, n + 1):
        total += i
    return total


def sum_formula(n):
    return n * (n + 1) // 2


print(sum_formula(100))
print(sum_loop(100) == sum_formula(100))
text
5050
True

The results match, but the number of steps differs:

python
def loop_steps(n):
    steps = 0
    for _ in range(1, n + 1):
        steps += 1
    return steps


for n in (10, 100, 1000):
    print(n, loop_steps(n), 3)
text
10 10 3
100 100 3
1000 1000 3

The third column is the operation count of the formula (about 3): it does not grow with n.

Why study algorithms

  • The right algorithm can turn hours of work into seconds.
  • Built-ins like sorted, dict and set are algorithms; it helps to know their cost.
  • Algorithmic thinking matters in interviews and in real projects.

The course path

For every problem we do the same: state it precisely, describe the idea in pseudocode, write Python code, check it with tests and edge cases, then measure speed and estimate complexity. Along this path we reach search, sorting, data structures and graphs.

⚠️ Do not confuse an algorithm with code: the same algorithm can be written in Python or in another language. Code is just one way of writing it down.