What Is an Algorithm?

Supporting explainer · Computer Science · 2 min read

A clear introduction to algorithms, with a short example and the idea of efficiency.

An algorithm is a finite, ordered sequence of clear steps for solving a problem or completing a task. A recipe, a set of directions and a long-division method are everyday examples.

Properties of a good algorithm

Ways to describe an algorithm

Example: find the largest number in a list

  1. Set largest to the first number.
  2. For each remaining number:
    • If it is greater than largest, set largest to it.
  3. After the last number, largest holds the maximum.

In Python:

numbers = [4, 9, 2, 7]
largest = numbers[0]
for n in numbers[1:]:
    if n > largest:
        largest = n
print(largest)  # prints 9

Searching: linear and binary

Efficiency

Computer scientists compare algorithms by how their running time or memory use grows as the input grows. This is often described with big-O notation. For example, linear search is O(n) and binary search is O(log n).

Common mistakes

Quick recap

An algorithm is a precise, finite series of steps. Describe it with steps, pseudocode or flowcharts, and judge it by correctness and efficiency.

Key vocabulary

Keep learning

More: Computer Science hub · Computer Science practice questions