In the digital world, we hear it so often, the algorithm changed or we should create an algorithm for that. The word is widely known, but can you explain what it means?

Wikipedia states that an algorithm “is a step-by-step procedure for calculations." Algorithms are used for calculations, data processing, and automated reasoning.

Let’s dive into this. Do you remember the old phone books? If not … you’re young, I’m jealous! I’ve put a picture of one here. A phone book was, back in the day, a thick book with thousands of pages containing names and phone numbers. This way you could find people and companies you wanted to call. Remember, there weren’t any smartphones yet.

Photo of phone book inside
Photo of phone book thickness

Understanding how an algorithm works

Say we want to look up the phone number of Phillip Pelton. Would it work if I would start on page one and just turn every page until I get there? … Definitely! This is already a step-by-step procedure we can call an algorithm. Is it very efficient? No, but it would get the job done.

Back in the day, we opened the phone book somewhere in the middle, looked at the Letter were we landed and moved on from there. That was our starting point. Can we translate this to make the algorithm smarter? Yes, we can.

Imagine if a phone book has 1268 pages. We can program the algorithm to split the number of pages into two groups. Now you have 634 pages, the algorithm can now check if letter P from Phillip Pelton is in the first or second group. It’s in the second group. Now we can do the same with the 634 pages that are left. Again we tell the algorithm to split the 634 pages into two groups, we now have groups of 317 pages. The algorithm again checks in which group the letter P is and in this case it’s in the second group.

I think you now have the hang of it, keep splitting the number of pages into groups with a yes or no choice.

1268
634
317
159
80
40
20
10
5
3
2
1 < Name ‘Phillip Pelton’

Back when we turned the phone book page by page, to get to the end name Phillip Pelton, it would have taken us turning probably around the 900 pages/decisions to make. In this case with the new decision making it took us and the algorithm (rounding the numbers up) 12 decisions to get there. You can imagine how much faster this is.

The success of algorithms

Algorithms are about problem-solving. It’s up to use to take our intuition and translate it in such a way that machines can understand.

Algorithm explanation time vs size of problem

The first line ‘n’ represents checking one page at a time, the second line ‘n/2’ represents turning two pages at the time. The second line is lower on the graph then first because it’s going faster. You can already see that turning two pages at the time is way faster than turning one page at the time.

Algorithm explanation step 2 time vs size of problem

With our third line ‘log n’ you can see the size of the problem doesn’t really matter anymore. This third line is the solution I showed earlier with splitting the number of pages into two groups 50%/50%.

Creating an algorithm

Let’s put the designs of the last algorithm into text.

1. Pick up phone book
2. Open to middle of phone book
3. Look at page
4. If Pelton is on page
5.      Call Phillip
6. Else if Pelton is earlier in book
7.      Open to middle of left half of book
8.      Go back to line 3
9. Else if Pelton is later in book
10.     Open to middle of right half of book
11.      Go back to line 3
12. Else
13.      Quit

These are the steps that are done in the algorithm. We can now see some kind of syntax in there as highlighted below are we’re going to name functions. These are verbs or actions that tell the computer, or in this case the human, what to do.

1. Pick up phone book
2. Open to middle of phone book
3. Look at page
4. If Pelton is on page
5.      Call Phillip
6. Else if Pelton is earlier in book
7.      Open to middle of left half of book
8.      Go back to line 3
9. Else if Pelton is later in book
10.     Open to middle of right half of book
11.      Go back to line 3
12. Else
13.      Quit

Down below I’ve highlighted what we call conditions. These are like forks in the road, you can choose to go with one of the options laid down for you.

1. Pick up phone book
2. Open to middle of phone book
3. Look at page
4. If Pelton is on page
5.      Call Phillip
6. Else if Pelton is earlier in book
7.      Open to middle of left half of book
8.      Go back to line 3
9. Else if Pelton is later in book
10.     Open to middle of right half of book
11.      Go back to line 3
12. Else
13.      Quit

In order to be able to make a choice, we need to ask questions. Those questions are what we call Boolean expressions. Boolean questions just have true or false answers or as you might know in binary 0 or 1.

1. Pick up phone book
2. Open to middle of phone book
3. Look at page
4. If Pelton is on page
5.      Call Phillip
6. Else if Pelton is earlier in book
7.      Open to middle of left half of book
8.      Go back to line 3
9. Else if Pelton is later in book
10.     Open to middle of right half of book
11.      Go back to line 3
12. Else
13.      Quit

On lines 8 and 11, there is a loop. A loop is a cycle that does something again and again and again.

1. Pick up phone book
2. Open to middle of phone book
3. Look at page
4. If Pelton is on page
5.      Call Phillip
6. Else if Pelton is earlier in book
7.      Open to middle of left half of book
8.      Go back to line 3
9. Else if Pelton is later in book
10.     Open to middle of right half of book
11.      Go back to line 3
12. Else
13.      Quit

We now have discussed four options: functions, conditions, Boolean expressions, and loops. There is more, there are also variables, threads, events, etc. but that’s something for another time.

I hope you now understand what an algorithm is and how it’s used. Would you like to know more about this subject than I can highly recommend doing the free course from Harvard called CS50: Introduction to Computer Science. Most of the information in this article is also told in that course, these are just my notes.

Front-end is a very important part of CRO, would you like to know more about what skills are needed for CRO? I’ve written an article about that.