awesome-everything RU
↑ Back to the climb

Algorithms from zero

What is an algorithm?

Crux An algorithm is a finite list of unambiguous steps that turns an input into an output.
◷ 16 min

You follow a recipe to bake a cake. Each step — mix the flour, add the eggs, bake at 350°F for 30 minutes — is an unambiguous instruction. The recipe works for anyone because no guessing is needed. That is a real-world algorithm.

Goal

After this lesson you can define an algorithm precisely, identify the input and output of a simple algorithm, and count the number of steps it takes to solve a problem.

The idea

An algorithm is a finite list of unambiguous steps that transforms an input into an output. The word “unambiguous” is load-bearing — every step must be clear enough that a computer (or a person) could execute it without any guesswork. The “finite” part means the steps must eventually end; an algorithm that loops forever is not an algorithm.

Consider a real algorithm: “Find the largest number in a list.” You start with an input — a list of numbers. You end with an output — the largest one. The steps in between are the algorithm.

1 function largestOf(numbers) {
2 let largest = numbers[0];
3 for (let i = 1; i < numbers.length; i++) {
4 if (numbers[i] > largest) {
5 largest = numbers[i];
6 }
7 }
8 return largest;
9 }
  • L1 Input: the list of numbers
  • L5 Core step: compare each number to the running largest
  • L8 Output: return the largest number found

Every algorithm has an input (what you start with) and an output (what you end with). The steps in the middle transform the input into the output. If the steps are unambiguous and finite, it is an algorithm.

The code

The algorithm “find the largest number in a list” is so common that we’ll code it in JavaScript. Here is the step-by-step logic:

  1. Assume the first number is the largest.
  2. Look at each remaining number.
  3. If a number is bigger than the current largest, remember it as the new largest.
  4. When you reach the end of the list, return the largest.

Here is the function:

function largestOf(numbers) {
  let largest = numbers[0];
  for (let i = 1; i < numbers.length; i++) {
    if (numbers[i] > largest) {
      largest = numbers[i];
    }
  }
  return largest;
}

Try running it with the list [3, 9, 2, 7]. The function will:

  1. Start with largest = 3 (the first number).
  2. See 9 — it is bigger, so largest becomes 9.
  3. See 2 — it is smaller than 9, so skip it.
  4. See 7 — it is smaller than 9, so skip it.
  5. Return 9.
Output
 
Step-by-step trace

Watch the algorithm step by step as it runs on the list [3, 9, 2, 7]:

1 function largestOf(numbers) {
2 let largest = numbers[0];
3 for (let i = 1; i < numbers.length; i++) {
4 if (numbers[i] > largest) {
5 largest = numbers[i];
6 }
7 }
8 return largest;
9 }

Complexity

How fast does the algorithm work? Count the steps: the algorithm visits each of the n numbers exactly once. For a list of 4 numbers, the loop runs 3 times (we skip the first). For a list of 100 numbers, the loop runs 99 times. For a list of n numbers, the loop runs n − 1 times.

We say the algorithm takes O(n) time, meaning the number of steps grows in proportion to the size of the input. If the list doubles in size, the number of steps roughly doubles.

Other algorithms for finding the largest are slower. For example, if you compared every number to every other number (checking if A > B, B > A, and so on), you would need n × n comparisons — O(n²) time. For large lists, this is much worse.

The key insight: the number of steps is the cost of an algorithm.

Practice 0 / 5

If the largestOf algorithm runs on a list of 10 numbers, how many comparisons does it make?

How many comparisons for a list of 50 numbers?

Which of these is an algorithm?

What is the time complexity of the largestOf algorithm?

Arrange these steps in the correct order for the largestOf algorithm:

  1. Set largest to the first number
  2. For each remaining number, check if it is bigger than the current largest
  3. If the number is bigger, update largest
  4. Return the largest
Edge cases

What if the list is empty? The algorithm will crash when trying to access numbers[0]. A more robust version checks if the list is empty first and handles it — perhaps by returning null or throwing an error. Real algorithms need to handle edge cases like this.

Check yourself
Quiz

Why is it important that the steps in an algorithm are unambiguous?

Recap

An algorithm is a finite list of unambiguous steps that transforms an input into an output. Every algorithm has a cost, measured by the number of steps it takes. We use big-O notation (like O(n) or O(n²)) to describe how the number of steps grows as the input size increases. The largestOf algorithm searches a list in O(n) time — visiting each element once.

Continue the climb ↑Is the algorithm correct?
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.