Course Introduction to Programming
Algorithm
In this lesson, we will learn about one of the fundamental concepts of programming: the Algorithm.
We will understand what it is, what its main characteristics are, see practical examples of algorithms in everyday life, and finally, understand the difference between an algorithm and a program, learning how a logical idea can be transformed into code that a computer executes.
Definition
An algorithm is an organized sequence of steps used to solve a problem or perform a task. We can think of it as a cake recipe:
- It describes step-by-step what must be done (mix ingredients, bake, wait, etc.)
- If you follow it correctly, the final result will be the finished cake.
- If you skip or swap any step, the result might go wrong.
Thus, an algorithm is the logic of the solution, a well-structured plan that explains how to get from a starting point to the desired result. You can write an algorithm in plain language, in pseudocode (an intermediate language between plain language and programming), or even represent it with flowcharts (drawings that show the execution flow of the steps).
Characteristics
The three main characteristics that make up a good algorithm are:
1. Sequence:
The steps or instructions have a defined order for execution, therefore, the order of the steps is of great importance to the final result of the algorithm.
- Example: you can't put the cake in the oven before mixing the ingredients.
2. Finiteness:
Every algorithm must have an end, meaning it cannot be infinite; it must reach a result after a limited number of steps.
- Example: the cake recipe ends when the cake is ready.
3. Simplicity:
Each step of the algorithm must be clear, direct, and possible to execute. Whoever follows the algorithm needs to understand what must be done.
- Example: โmix the ingredientsโ is a clear instruction. On the other hand, โdo what's necessaryโ is vague and not helpful.
Examples of algorithms:
Here are some examples of algorithms in real life to facilitate understanding:
- Changing a broken light bulb
- Remove the broken light bulb
- Put in a new light bulb
- Making a glass of chocolate milk
- Pour 250ml of milk into a glass
- Add two tablespoons of chocolate powder
- Mix the ingredients
- Opening a door
- Stand in front of the door
- If the door is closed, open it
- Go through the door
- Close the door
Note that, in all cases, there is a finite number of steps. However, these algorithms will only be effective if the person performing these actions knows how to carry out all the steps of the algorithm. If someone doesn't know how to open a door or remove a broken light bulb, the algorithm becomes useless.
Furthermore, note that in algorithm 2, whether the person adds the milk or the chocolate powder first, the result will be the same. Similarly, in many algorithms, there can be steps that can be executed regardless of their order. However, unless you want to hit your head trying to go through a closed door, you must pay close attention to the order of the steps in an algorithm.
Algorithm vs. Programs
The algorithm is the idea, the plan, the logic for solving a problem. It can be written in plain language, pseudocode, or shown in a flowchart. It is the reasoning that explains what must be done and in what order.
The program, on the other hand, is the algorithm translated into a programming language, such as Python, C, Java, among others. In other words, it is the version of the algorithm that the computer can understand and execute.
Conclusion
Throughout this lesson, you learned what an algorithm is, got to know its main characteristics, such as sequence, finiteness, and clarity, saw practical examples from everyday life, and understood the difference between an algorithm and a program, comprehending that the former represents the logic of the solution, while the latter is that logic translated into a language the computer understands.
Now you know how to plan and structure the reasoning behind a program, creating well-organized and efficient solutions ๐
