What Is an Algorithm? — Algorithms

An algorithm is an ordered list of clear steps that always finishes and can be carried out, from input (the data that goes in) to output (the result). It is…

Five cards lie face down on the table, and each card has one number on it. Your job: find the biggest number. You turn over the first card and remember its number. Then you turn over the second card. If its number is bigger, that is the number you remember now. You do the same up to the fifth card. A card you have not turned over could hold the biggest number, so you have to turn over all five.

That way of turning over the cards is an algorithm: an ordered list of clear steps that solves one problem and always finishes. One step is one small job that can be done right away, like comparing two numbers or storing one value. An algorithm is like a recipe: the ingredients that go in are the data, you follow the steps one by one, and the dish that comes out is the result.

Example: find the biggest number

The problem: in the list of numbers [3, 7, 1, 9, 4], which number is the biggest? Here is the algorithm, written in words:

  1. Take the first number as the biggest for now. Store that number as max.
  2. Take the next number. If that number is bigger than max, replace the value of max with it.
  3. Repeat step 2 until you have taken every number. Then return max as the answer.

This table shows how the algorithm runs, step by step. i is the position number (the index) of the number taken, counted from 0:

Step What is compared max afterwards
start take the first number, 3 3
i = 1 is 7 bigger than 3? yes 7
i = 2 is 1 bigger than 7? no 7
i = 3 is 9 bigger than 7? yes 9
i = 4 is 4 bigger than 9? no 9

The same algorithm, written in JavaScript as a function named findMax:

function findMax(arr) {
  let max = arr[0]; // assume the first is the biggest
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > max) max = arr[i];
  }
  return max;
}

console.log(findMax([3, 7, 1, 9, 4])); // 9

The last line runs findMax with the five numbers and prints the result: 9. All five numbers are looked at, just like the five cards at the start: a number you have not looked at yet could be the biggest.

What makes a proper algorithm

An algorithm is not tied to one programming language: the same steps can be written in JavaScript, in Python, or on paper.

Big-O: how many steps when there's a lot of data

One problem can be solved by many algorithms. The better algorithm is usually the one that needs fewer steps when there is a lot of data. The amount of data is written n.

findMax compares each number once, except the first: 5 numbers take 4 comparisons, and 1,000 numbers take 999. The number of steps grows in proportion to n, and this is written O(n) (read "big O of n"). It means: if the data doubles, the time roughly doubles. This way of writing is called Big-O. Big-O only looks at how the number of steps grows, not at the exact count, so 999 steps is still written O(n).

The five Big-O classes you will meet most, from fastest to slowest:

Big-O What it means Steps for 1,000 items
O(1) the same, however much data there is 1
O(log n) double the data, and it takes just one more step about 10
O(n) double the data, double the steps 1,000
O(n log n) a bit heavier than O(n) about 10,000
O(n²) double the data, four times the steps 1,000,000

log n is how many times n can be halved until 1 is left. For example, 1,000 can be halved about 10 times. n² (n squared, which is n times n) usually comes from a loop inside a loop.

The difference grows as the data grows. For 10,000 items, an O(n log n) algorithm takes about 130,000 steps, while an O(n²) one takes 100 million steps. So the O(n log n) algorithm is far faster, in any language.

⚠️ What people miss when judging an algorithm

TL;DR: An algorithm is an ordered list of clear steps that always finishes and can be carried out, from input (the data that goes in) to output (the result). It is not tied to a programming language. One example is findMax, which finds the biggest number: store the first number, replace it when you find a bigger one, and return it after you have seen every number. Big-O says how fast the number of steps grows as the amount of data (n) grows. When the data doubles: O(1) stays the same, O(log n) takes one more step, O(n) doubles, and O(n²) becomes four times as big. On a lot of data, O(n log n), which is a little heavier than O(n), is far faster than O(n²).

⚡ An algorithm is a recipe

Write:

function findMax(arr) {
  let max = arr[0]; // assume the first is the biggest
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > max) max = arr[i];
  }
  return max;
}

console.log(findMax([3, 7, 1, 9, 4])); // 9

Meaning: findMax finds the biggest number in the list arr: store the first number as max, replace max when you find a bigger number, then return max after every number has been looked at. An ordered list of clear steps is called an algorithm. An algorithm is like a recipe: the ingredients are the input (the data that goes in), and the finished dish is the output (the result).

⚡ What makes a proper algorithm

Meaning: A proper algorithm has input and output, every step is clear (arr[i] > max, not "a number that looks quite big"), it always finishes, and it can be carried out (it only uses basic jobs, such as comparing). An algorithm is also not tied to one programming language.

⚡ Big-O: O(n)

Meaning: findMax compares each number once, except the first: 5 numbers take 4 comparisons, and 1,000 numbers take 999. The number of steps grows in proportion to the amount of data (n). Big-O is a way of writing that growth, and findMax is written O(n): if the data doubles, the time roughly doubles.

⚡ O(n log n) against O(n²)

Guess first: for 10,000 items, an O(n log n) algorithm takes about 130,000 steps. How many steps does an O(n²) algorithm take?

100 million steps: n² means n times n. So the O(n log n) algorithm is far faster, in any language.

Meaning: n² usually comes from a loop inside a loop. O(n log n) is only a little heavier than O(n). The more data there is, the bigger the gap between O(n log n) and O(n²).

What you will learn