The word “algorithm” is thrown around a lot today with respect to computer science and programming - but what exactly are algorithms? The word may sound complicated, but the concept is relatively easy to learn even without a coding background.
In this article, we’ll examine algorithms, learn how to create them, and discuss algorithms’ use in real life. When you're ready to jump right into learning all about algorithms, join our award-winning free live online AI class for kids, designed by experts from Google, Stanford, and MIT.
What is an algorithm?
Simply put, an algorithm is a set of steps used to solve a specific problem. While algorithms often appear in computer science or coding contexts, an algorithm can be as straightforward as the process for making a peanut butter and jelly sandwich. Once you’ve written out the steps for making a PB&J, you can use this same algorithm every time you want to make a new one, and you can share your algorithm with other, hungry people.
What are algorithms in coding?
Algorithms in coding are lines of code, built into blocks or functions, that can solve programming or mathematical problems. They may be written in any language. For a piece of code to be considered an algorithm, it must consistently produce the right solution and must be guaranteed to work on reasonable inputs. Algorithms in coding may be simple methods that can search for information or sort lists. More complicated algorithms can be used for artificial intelligence, data science, and more!
Coding algorithms are especially useful because they can solve big problems much more quickly than humans can. In fact, certain problems that are too complex to solve mathematically can be solved with algorithms.
An algorithm in coding example
Below is a simple example of an algorithm, written in Python:
This example creates a function, findMax(), which finds the biggest number in a list of numbers. Essentially, this algorithm looks at every number in the list and if the next number is bigger than the current maximum value, sets the maximum value to the bigger number. This algorithm is guaranteed to work, even on a very big list.
Another common problem that may be solved with algorithms is the Traveling Salesman problem. In this challenge, the objective is to find the shortest path between multiple cities. While this problem can be extremely complicated to do “by hand” when there are a large number of cities, computer algorithms can solve it very quickly!
There are some computer algorithms you likely use every day. Facebook, Instagram, Twitter, and many other social media platforms rely on algorithms to serve you relevant posts and recommend other content. Google Search sits on a powerful algorithm which helps you find specific information among the billions of pages of the internet.
Algorithm examples in everyday life
We apply algorithms in real life without even thinking about it. We’ve listed some examples below.
Baking a cake
The process of baking a cake can be broken down into gathering ingredients, preheating the oven, mixing wet and dry ingredients together, filling a cake pan, and then baking until done. This type of algorithm could be documented in a cookbook, or an online recipe site.
Tying your shoes
Tying your shoes is an algorithm that is likely so automatic you don’t even think of the steps as you do them. Many routines or habits are algorithms, as they can be broken into steps that you repeat every time you need to complete the task.
Driving a car
Learning how to drive a car means learning lots and lots of rules of the road. Most of the actions we perform as we drive can be thought of as algorithms. The proper process for moving through a four-way stop, changing lanes, and even parking all have a set of specific steps that make them algorithms.
What other examples can you think of?
Types of algorithms
The most basic types of computer science algorithms are:
- Brute force algorithms: The simplest possible algorithm that can be devised to solve a problem. It's like using all possible combinations of numbers to open a bank safe.
- Backtracking algorithms: This is a slight improvement on the brute force algorithm, and is a technique to find a solution to a problem in an incremental approach. It starts with one possible option out of many available. Then tries to solve the problem. If it can solve it with the selected move, then it uses the solution. Otherwise, it backtracks and selects something else to try to solve it.
- Divide and conquer algorithms: This algorithm divides the problems into subproblems and then solves each of them, before combining them to form the solution to the given problems. Begin by dividing the algorithm into two parts; the first parts divide the problem on hand into smaller subproblems of the same type. In the second part, these smaller problems are solved and then added together (combined) to produce the problem’s final solution.
- Dynamic programming algorithms: This is one of the most efficient ways to solve a problem: it remembers the results of the past run and uses them to find new results.
- Greedy algorithms: In this type of algorithm, a decision is made that is good at that point without considering the future. These algorithms are used for solving optimization problems.
- Randomized algorithms: This is an algorithm type that makes its decision on the basis of random numbers. Basically it uses random numbers in its logic.
- Recursive algorithms: This is an algorithm that calls itself repeatedly until the problem is solved. It thinks about the existing cases and the solution of the simplest subproblem, and all other complexity will be handled by it automatically.
Where are algorithms used in computer science?
Algorithms are the building blocks for programming. So a big part of computer programming is learning about the formation and analysis of algorithms. Today, algorithms allow computers, smartphones, and websites to function and to make decisions. As just one small example, if you use apps such as Facebook and TikTok, the algorithms on those platforms help chose the content that gets shown to you.
How do you write a code algorithm?
The process for writing an algorithm can be broken down as follows:
1. Write it in human-speak.
Write the algorithm out in human-speak or as a flow chart. Don’t try to code your algorithm before you truly understand what it needs to do. Spend some time figuring out the process and all the different cases your algorithm may need to handle.
Break your process into very specific steps, still in human-speak. We’ll later translate these steps into a programming language. Often, this can be done as comments in your coding environment. Having a good frame for your algorithm will make it that much easier to code.
3. Code it!
Using your pseudocode, write out each step in your algorithm in your coding language of choice.
4. Test it.
Test your algorithm. Make sure you try as many different cases as you possibly can, so that your algorithm can handle lots of different types of inputs.
Astute readers will see that the process for writing an algorithm is, in fact, an algorithm.
Now you know what an algorithm is!
By now, you know more about algorithms, how they are used in real life and in computer science, and how to code a basic algorithm. As you can see algorithmic thinking can help children develop lifelong logic thinking skills. If you’re interested in learning more, check out our AI Explorers class where we discuss many different types of AI algorithms and how they can be used to teach computers to learn!
Written by Sarah Rappaport, who graduated from Northwestern University with undergraduate and graduate degrees in engineering and music. She's now working on a masters in data with Georgia Institute of Technology. She taught math and computer science with Teach for America for two years, and now works as a Systems Engineer.