What is an algorithm

You already use algorithms · Part of Complexity and data

Key idea

Practice

7 questions · about 4 min

Explained step by step

Algorithm

An algorithm is a recipe: a list of steps that, followed in order, solves a problem. It has an input (the ingredients), an output (the dish) and a finite number of steps. For example, "add the numbers from 1 to 100" is a problem, and "add 1 number at a time, 100 times" is an algorithm that solves it. You don't need a computer: a person following the recipe is also running an algorithm.

To be an algorithm, something must meet 3 conditions. First, the problem is clear: you know what goes in and what comes out. Second, it ends: it uses a finite number of steps, time and memory. Third, each step means exactly one thing: with the same input, it always gives the same output. It's like a calculator: 2 + 3 gives 5 today, tomorrow and all 1,000 times you try it.

Binary search

Looking up a word in a paper dictionary is, without knowing it, binary search. You open roughly in the middle, look at the letter on that page and, because words are in alphabetical order, you know whether yours comes before or after. You throw away the half that doesn't help and repeat. With 1,000 pages, in about 10 steps you reach the right page. It only works because the dictionary is sorted.

Insertion sort

When you sort the cards in your hand, you're using insertion sort without noticing. You keep the sorted cards on the left and, one at a time, take the next card and slide it into place, shifting the bigger ones. With 5 cards it's super fast. With lots of data it gets slow, because each new card may have to pass all the previous ones, but for small lists it's one of the best.

Greedy algorithm

A greedy algorithm picks, at each step, what looks best right now, without thinking about the steps ahead. To give 31 in change with bills of 1, 5, 10 and 20, the cashier gives the largest that fits: 20, leaving 11; then 10, leaving 1; then 1. Total: 3 bills. It's fast and simple, and with common coins it gives the best answer, though that doesn't happen with every problem.

Data structure

A data structure is the way data is organized in memory: how it's stored, how it's related and what you can do with it. It's like choosing between a pile of papers, a filing cabinet with folders or a family tree: the same data, organized differently, is useful for different things. None is best at everything: a linked list adds an element in 1 step but takes a while to reach element 500; an array gets there in 1 step but inserting in the middle forces it to shift everything.

Data structures and algorithms work together, like the pieces of a building-block set and its instruction booklet. The loose pieces are the data; how they're organized (shapes, sizes, how they fit) is the data structure; the steps to build the model are the algorithm; and the finished model is the output. The same algorithm can work with different structures, but the speed changes a lot: searching 1 million sorted items takes 20 steps, unsorted up to 1 million.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

You already use algorithms