Algorithms from zero
What is an algorithm?
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.
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.
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 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:
- Assume the first number is the largest.
- Look at each remaining number.
- If a number is bigger than the current largest, remember it as the new largest.
- 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:
- Start with
largest = 3(the first number). - See
9— it is bigger, solargestbecomes9. - See
2— it is smaller than9, so skip it. - See
7— it is smaller than9, so skip it. - Return
9.
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
}
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.
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:
- Set largest to the first number
- For each remaining number, check if it is bigger than the current largest
- If the number is bigger, update largest
- 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.
Why is it important that the steps in an algorithm are unambiguous?
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.