Why Do You Need to Learn Algorithms?

Published in

--

What do you think when you hear the word “algorithm”? It’s a word that is impossible to avoid in our modern world, but few people really know what it means.

It is in fact a rather vague term, usually, it means a sequence of actions where the main goal is to solve a problem. Do you know how to bake a cake, how to cross a road, or how to find the book you’re looking for in Barnes & Noble? Those are algorithms. But in the programming world, we are talking about a different kind of algorithm. Why is it important to learn them and know how to use them? Maybe we are just wasting our time and could learn a new framework instead of spending hours trying to understand all this complicated stuff?

Let’s see.

Throughout the history of computer science, there has been an understanding that algorithms and data structures are an essential part of programming and are necessary to solve practical problems, it is something that every developer MUST know.

For example, think of a sorting algorithm — you write some software for a grocery store where goods have to be sorted by price or by expiration date, or an app which helps a user find a hair salon, requiring you to sort salons by distance or by their user ratings. Surely knowing which sorting algorithm to choose and how to implement it would be helpful (Wikipedia gives us 44 sorting algorithms, and that’s just the tip of the iceberg.)

ALGORITHMS HELP TO FIND THE MOST EFFECTIVE SOLUTION TO A PROBLEM

Imagine you want to go pick up some food from your favorite restaurant. There are three different routes you can take: use the sidewalk which runs along the highway (it’s a long way and dangerous), cutting through the back alleys (fast way but still dangerous) or taking the bus (fast and safe but you have to pay). There might even be a different solution to this problem, maybe you could take your car, or just order delivery. You have to find the most effective way to get your food.

So, let’s bring this into the programming world. A developer has to find the most effective solution: they have to consider how fast a program works, the program’s memory usage, how easy it is to implement a solution and even how much money their company has to spend to get the final version of the software.

For example, you need to sort an array of n numbers in ascending order.

This looks so simple! Firstly, you look through the entire array, find the smallest number and swap it with the first element of the array. Second step: you check numbers again starting from the second element of the array, pick the smallest one and swap it with the second element in the array, and so on — you will repeat this process n-times. This is Selection Sort. Sure, you get your numbers sorted but it takes a long time, and if the array contains a few billion numbers, your program will be running for quite a while. To understand which algorithm will be the most efficient in this case we need to know how other sorting algorithms work.

Here is another example. You learned to write code, and now you are a Ruby Wizard but you know nothing about algorithms. You have created a video hosting site (you hope the next YouTube). Your project is super successful but now you’ve got 10,000 people signed-in at the same time and everything is starting to break down. Your servers can’t handle so many requests at the same time and start failing. Users are complaining that videos are taking forever to upload. Well, now you have to think of a solution and implement the most effective compression algorithm you can find. To do this you need to know how algorithms work.

Knowing what is going on under the hood helps you to understand why solution x is better than solution y. Once you get a grip on the most fundamental algorithms, you will be able to write your own or combine them to solve more complex problems.

The biggest tech companies, such as Google, Facebook, and Amazon, conduct separate interviews focused specifically on data structures and algorithms. These companies are looking for a person who will be able to design things quickly using the most effective algorithms to save the company’s resources (computation power, servers, money, etc.). An example question you might expect to hear while interviewing with these companies might be something like this:

You are expected not only to find a solution to the problem but also to explain why you chose algorithm A over algorithm B.

The main task of a developer is to analyze and solve problems, where code is just a tool to achieve a final goal. Google search would not be so efficient if not for its unique and ground-breaking use of algorithms. While other search engines at the time were simply looking for keywords, Google realized what really mattered was context, usage, and inter-connectivity. Not what it looks like but what it actually means.

ALGORITHMS HELP TO TRAIN YOUR BRAIN

Once you really begin to understand the logical structure of algorithms you will likely find that you don’t just use them at work. They are like a good pair of running shoes for your brain.

Steve Jobs said,

“Everyone should learn to program a computer, because it teaches you how to think.”

Almost every part of your day-to-day life can be optimized and improved by developing a logical structure to save yourself time and get better results, whether it’s finding the best route to work, getting the best parking space in the shortest amount of time, doing your grocery shopping as quickly and cheaply as possible or getting the best deal on an airline ticket, algorithms are powerful tools that teach you how to express your thoughts and solve seemingly unsolvable problems.

There are a lot of great websites out there where you can practice your algorithms solving skills. I recommend checking out some of these sites:

--

--