# 2.1. Algorithms¶

Have you ever alphabetized a stack of papers or a list of words? Did you sing the alphabet song while you were performing the task? Did you sort the words into groups based on the first letter? Did you “fan” the papers so you could look at all of the names as you organized the stack? Did you get help from a partner and split the task in half?

Regardless of how you completed the task, you probably followed a pattern to make the process easier. If you had to repeat the task with a new set of words, you could jump right in and follow the same pattern.

The word algorithm is just a fancy name for a pattern or set of steps that solve a specific problem.

## 2.1.1. Algorithms Are Easy to Find¶

Imagine a recipe for baking cookies. After the list of ingredients comes a series of step-by-step instructions for making the treats. If you want to cook something else, like a cake or a roast, you follow a different set of steps using a different set of ingredients.

An algorithm is like a recipe. It is a careful series of steps that, when followed, produce a specific result. Programmers create algorithms to accomplish small tasks. By combining many separate, small tasks, programmers build larger applications.

### 2.1.1.1. For Example¶

Let’s take a look at alphabetizing a list of words:

`apple, pear, zebra, box, rutabaga, fox, banana, socks, foot`

Here is one way to complete the task:

1. Arrange the words from a - z based only on the first letter:

`apple, box, banana, fox, foot, pear, rutabaga, socks, zebra`

2. If more than one word starts with ‘a’, rearrange those words based on the second letter. Repeat for the words that start with ‘b’, then ‘c’, etc.:

`apple, banana, box, fox, foot, pear, rutabaga, socks, zebra`

3. If multiple words start with ‘a’ and have the same second letter, rearrange those words based on the third letter. Repeat for the ‘b’ words, then the ‘c’ words, etc.:

`apple, banana, box, foot, fox, pear, rutabaga, socks, zebra`

4. If other repeats exist, continue sorting the list by comparing the 4th, 5th, 6th letters (etc.) until all the words are properly arranged.

This is not the ONLY way to solve the task, but it provides a series of steps that can be used in many different situations to organize different lists of words.

Alphabetizing is a process we can teach a computer to do, and the algorithm will complete the process much more rapidly than a human.

## 2.1.2. Algorithms Do Many Things¶

Cookbooks contain pages and pages of algorithms. Following 2 or 3 of these produces a nice dinner plus dessert. Programmers use this same idea, only they work with devices instead of food.

Every algorithm is designed to do one small job. Combining different algorithms together allows programmers to solve much more complicated problems.

1. Have you ever used the “You may also like…” option when looking at movies or books online? Algorithms take your past choices and use them to recommend new titles.

2. In 2019, astronomers took X-ray data collected by NASA and used algorithms to create the first image ever taken of a black hole.

https://www.nasa.gov/mission_pages/chandra/news/black-hole-image-makes-history

3. The apps on a phone are just combinations of algorithms working together to do a job. Applying a filter to a photo, playing a game with users across the world, or just calling mom all result from carefully designed sets of instructions.

Programmers can do so many things with computers, but the devices are useless unless we give them algorithms to follow.

## 2.1.3. Check Your Understanding¶

Question

An algorithm is:

1. A solution to a problem that can be solved by a computer.
2. A step by step list of instructions that if followed exactly will solve a problem.
3. A single command run by a programming language.

Question

Select ALL of the following that can be solved by using an algorithm:

1. Answering a math problem.
2. Sorting numbers in decreasing order.
3. Making a peanut butter and jelly sandwich.
4. Assigning guests to tables at a wedding reception.
5. Creating a grocery list.
6. Suggesting new music for a playlist.
7. Making cars self-driving.