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 free live online AI class for kids.
What are algorithms?
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?
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 algorithms in computer science are!
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. 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! Sign up for our free AI introductory course.
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.