AP®︎/College Computer Science Principles
The building blocks of algorithms
An algorithm is a step by step process that describes how to solve a problem in a way that always gives a correct answer. When there are multiple algorithms for a particular problem (and there often are!), the best algorithm is typically the one that solves it the fastest.
As computer programmers, we are constantly using algorithms, whether it's an existing algorithm for a common problem, like sorting an array, or if it's a completely new algorithm unique to our program. By understanding algorithms, we can make better decisions about which existing algorithms to use and learn how to make new algorithms that are correct and efficient.
An algorithm is made up of three basic building blocks: sequencing, selection, and iteration.
Sequencing: An algorithm is a step-by-step process, and the order of those steps are crucial to ensuring the correctness of an algorithm.
Here's an algorithm for translating a word into Pig Latin, like from "pig" to "ig-pay":
1. Append "-". 2. Append first letter 3. Append "ay" 4. Remove first letter
🔍 Try following those steps in different orders and see what comes out. Not the same, is it?
Selection: Algorithms can use selection to determine a different set of steps to execute based on a Boolean expression.
Here's an improved algorithm for Pig Latin that handles words that starts with vowels, so that "eggs" becomes "eggs-yay" instead of the unpronounceable "ggs-eay":
1. Append "-" 2. Store first letter 3. If first letter is vowel: a. Append "yay" 4. Otherwise: a. Append first letter b. Append "ay" c. Remove first letter
Iteration: Algorithms often use repetition to execute steps a certain number of times or until a certain condition is met.
We can add iteration to the previous algorithm to translate a complete phrase, so that "peanut butter and jelly" becomes "eanut-pay utter-bay and-yay elly-jay":
1. Store list of words 2. For each word in words: a. Append hyphen b. If first letter is vowel: i. Append "yay" c. Otherwise: i. Append first letter ii. Append "ay" iii. Remove first letter
By combining sequencing, selection, and iteration, we've successfully come up with an algorithm for Pig Latin translation.
🤔 Can you think of situations where it produces an incorrect output?
🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer—just ask in the questions area below!
Want to join the conversation?
- I had to look up pig Latin in dictionary, may be a more internationally understood concept could be used as example for better understanding.(45 votes)
- You are really right! Why make things weirder?(7 votes)
- This is very confusing to me!! I do not understand the difference between the three (iteration, sequencing, and selection) can you help me?(12 votes)
- Are you familiar with programming? A programming background helps to understand, since they map to programming concepts. Sequencing is a part of all programs, as it's just the fact that computers execute programs in a particular sequence (like top to bottom line in a simple program). Iteration is when we use loops to repeat code in a program. Selection is when we use conditionals (if/else) to execute different blocks of code in a program.(38 votes)
- Would something go wrong if the list of words is initially empty?(1 vote)
- From the author:From the way I read it, an empty list is compatible with the pseudocode. It would get to step 2, see that there are no words in the list, and not execute any of the operations under step 2.
That's a great case to consider when you're turning this pseudocode into real code, however! I have very often had bugs in code arise from my failure to consider the case of an empty list.(11 votes)
- What's the difference between an algorithm and a program?(1 vote)
- Hi Chawin!
An algorithm is a way to condense code in such a way that you are able to use that algorithm to have a more efficient program. A program is the general code that creates an output.(11 votes)
- In fact I'm totally confused with the use of Pig Latin in the explanation of the building blocks of Algorithm. I know programming, but this seems strange to me(5 votes)
- I'm really confused. I didn't get the analogy with pig latin. Can someone please help?(1 vote)
- There are three building blocks of algorithms: sequencing, selection, and iteration. Sequencing is the sequential execution of operations, selection is the decision to execute one operation versus another operation (like a fork in the road), and iteration is repeating the same operations a certain number of times or until something is true. Which of those building blocks is confusing to you?(6 votes)
- Would something go wrong if you put semi-step "a" below semi-step "b"?(1 vote)
- Hm, wouldn't that put the hyphen after the yay? andyay-? (Or am I misunderstanding what step you're referring to?)(5 votes)
- I somewhat get it. The pig Latin part does confuse me though.(2 votes)
- How many algorithms the it take to crash your computer?(1 vote)
- you only need one, just use an iteration but you put the wrong number (more than the list) -if you do an iteration manually you have to input the number of object inside a list so that the program can iterate each item correctly- If you put the number beyond the number of item inside the list, the program try to look for an item that isn't exist. As a result, the computer will crash.
or you put a loop program that never end, when there is no condition where the computer will have to stop the task, it will also crash.
try it, and let me know your exerience(3 votes)
- Tips on how to identify the appropriate algorithm?(0 votes)
- Practice, the more algorithms you know and the more situations in which you have implemented algorithms, the easier it will be to find the appropriate algorithm. Or if there isn't a good one you can figure out how to build your own.(5 votes)