Module 2 Unit 1: What is an algorithm?#
Unit Learning Outcomes#
By the end of this unit, you will be able to
State what an algorithm is
Give examples of algorithms, both simple and complex.
Algorithms don’t have to be connected to computers. For instance, the cake recipe we discussed in the previous module is an algorithm. A recipe is a list of instructions for a cook to follow in order to complete the task of creating a cake.
Directions on how to knit a sweater using knitting needles and a length of wool is another example of an algorithm.
The actions you take each morning to get ready to leave the house or to sort which textbooks to take to class for the day can be represented as algorithms.
The steps in the algorithm have to be clear enough that another person (or computer) can follow the steps unambiguously. How detailed the steps should be can depend on context. For instance, a recipe may call for “a pinch of salt” which is good enough for an experienced cook to understand. For a less experienced cook, it would be better to be more precise: use 1⁄8 teaspoon of salt.
There is no single correct “algorithm” to complete a task. There is often a choice. For instance, if you want to sort your textbooks to put into a knapsack, it might make sense to sort them by size. If you want to put your textbooks on a bookshelf, you might want to sort them by subject matter, or alphabetically by author.
Algorithms become especially important when we are dealing with lengthy or complex tasks, as they provide a GENERAL and ABSTRACT solution to a problem that would take too long to solve uniquely each time. When resolving these type of tasks the goal is to choose an algorithm that will be fast and efficient.
For instance, if you want to sort 10 textbooks it probably won’t take long, no matter what method you use. If you work in a library and have to sort 10,000 books, it would be wise to find an efficient algorithm. A poor algorithm will take you a long time!
Computers, of course, spend a lot of time sorting data, so computer scientists worry a lot about efficient sorting algorithms. These ideas can even help in the classroom – if you’ve ever had to sort 100 student exam papers alphabetically, you probably have realized that there are fast ways to do this, as well as slow ways!
Many important computational tasks are like this—some need fast algorithms to be useful.
History Note
The word “Algorithm” comes from the name of 9th-century Persian scholar Muḥammad ibn Mūsā al-Khwārizmī. Formal algorithms have been used as far back as the ancient Egyptians and ancient Greeks. They used them for various mathematical calculations, such as computing areas of pieces of land, or finding prime numbers.
Activity#
Follow the prompts below to begin practicing computational thinking
Making pancakes#
Imagine writing down a recipe for making pancakes. Can you write instructions with enough detail that an 8-year-old could follow them successfully?
If you wanted to sort 10 books on a bookshelf into alphabetical order, how might you do it?
If you wanted to sort 10,000 books, would your method be the same? Can you think of a way to organize the books that doesn’t begin with removing them all from the shelves?
Instructions that are made up of repeated actions are known as a “loop.” For example, knitting instructions might contain a step like “cast on twelve stitches.” Look up instructions on how to knit a scarf. What other examples of loops do you see?
The following algorithm indicates if a number is even: take any number and divide it by two. If the result is a whole number, then the original number was even. Can you think of a faster algorithm?
Note
Algorithms can be used as the starting point for instructions for a computer, which can often do the steps much faster than a human. We’ll explore this in the next unit.