Due to how our culture talks about the term algorithms, it's easy to be under the misconception that you need to be a genius to understand algorithms. However, this is inaccurate. The term algorithm refers to a set of instructions used to do something. In this lesson, you'll learn more about the term algorithm and begin to write your algorithms in plain English.
By the end of this lesson, you should be able to:
- Define the term algorithm.
- Write algorithms in plain English to solve everyday problems.
An algorithm is a set of instructions that can be used to do something or anything. Your daily life is full of algorithms, even though you likely don't call them that. Excellent algorithms are efficient, repeatable, and elegant. However, these aren't requirements.
Watch the first half of the video below (up until about 2:19), which describes algorithms and gives a few different examples. The example with the phone book may be challenging to understand. If you need to, watch the video a couple of times until you understand the general concept.
In the example above, two examples were mentioned:
- An algorithm for sorting your laundry.
- An algorithm for finding a name in a phone book.
While these algorithms can be written in code, they can also be described in plain language.
You likely follow all kinds of instructions in your daily life. For example, whenever you follow a recipe, you follow a list of instructions with an intended outcome. Take a look at the following recipe for chocolate chip cookies.
Preheat oven to 350 degrees Fahrenheit.
Then, cream together the butter, white sugar, and brown sugar until smooth.
Beat in the eggs one at a time, then stir in the vanilla.
Dissolve baking soda in hot water.
Add to batter along with salt.
Stir in flour, chocolate chips, and nuts.
Drop by large spoonfuls onto un-greased pans.
Bake for about 10 minutes in the preheated oven or until the edges are nicely browned.
The above description includes several steps to follow. These steps could be rewritten to look even more like a step-by-step process.
1. Preheat oven to 350 degrees Fahrenheit.
2. Cream together the butter, white sugar, and brown sugar until smooth.
3. Beat in the eggs to the butter and sugar mixture one at a time.
4. Stir the vanilla into the mixture.
5. Dissolve baking soda in hot water.
6. Add baking soda to the batter along with salt.
7. Stir in flour, chocolate chips, and nuts.
8. Drop batter by large spoonfuls onto un-greased pans.
9. Bake for about 10 minutes in the preheated oven or until the edges are nicely browned.
Each of these individual steps might even be able to be broken down further. Regardless, you now have a series of sequential steps that can be followed to create chocolate chip cookies.
There's one component missing from the algorithm above: ingredients! Like with a recipe, many algorithms require some input to create their output.
❗️ What is an algorithm you use?
Think of the daily tasks you do throughout the day. What might be described as an algorithm? Take a moment to think through the steps you follow and the desired outcome.While not required, almost every algorithm uses some input. That input is used to help the algorithm accomplish its intended goal or output. In the example above of baking cookies, the input for the recipe is the actual ingredients list.
- 1 cup butter, softened
- 1 cup white sugar
- 1 cup packed brown sugar
- 2 eggs
- 2 teaspoons vanilla extract
- 1 teaspoon baking soda
- 2 teaspoons hot water
- ½ teaspoon salt
- 3 cups all-purpose flour
- 2 cups semisweet chocolate chips
- 1 cup chopped walnuts
The ingredients above are required in the specified amounts to create the correct output of a delicious chocolate chip cookie. If those ingredients are skewed, the desired output may not be achieved.
To write an algorithm in plain language, you should answer the following questions:
- What is the desired output of this algorithm?
- What inputs are required to achieve this outcome?
- What set of steps need to be followed in order to achieve the output?
Once you've answered these questions, you should write a set of instructions that is as precise as possible. For example, if you were writing an algorithm for how to make a peanut butter and jelly sandwich, you might write a step that sounds like this:
Spread jelly and peanut butter on the bread.
However, this step is unclear. Do you spread both the jelly and peanut butter on the same piece of bread? Does it matter which side you spread it on? A better step may read like this:
Spread jelly on one piece of bread, filling the surface area of the
bread's crumb instead of its crust. Spread the peanut butter on
the other piece of bread in a similar fashion.
The above reads more precisely. Precise algorithms have a better chance of resulting in your intended outcome.
❗️ Write an algorithm
Using either the algorithm you came up with earlier or a different one, write out a detailed series of steps to produce the desired outcome. Don't forget to include inputs if needed.Recall this step from earlier that was part of the algorithm for baking chocolate chip cookies.
9. Bake for about 10 minutes in the preheated oven or until the edges are nicely browned.
This step allows for an edge case. An edge case is a situation that occurs during the process of executing an algorithm. While the situation is unlikely, it needs to be accounted for. In this case, the recipe accounts for the fact that some ovens might run hot or the baker may have used too much or too little batter. Generally, you should bake for 10 minutes when following the above algorithm. But, if the edges are nicely browned, the process changes slightly.
Edge cases are important to keep in mind when thinking about algorithms. For example, when following directions to a new location, you may need to keep in mind any road closures or inclement weather. When separating laundry, you may have a situation where you have no whites to separate.
Thinking of these edge cases can quickly slow down the development of an algorithm. Developers often refer to the "happy path" when designing algorithms. The "happy path" references the most typical scenario for an algorithm. In the cookie example above, the "happy path" could be referred to as the cookies being perfectly baked for 10 minutes exactly.
When designing algorithms, design for the "happy path" and then consider any edge cases.