Algorithms is a main topic in programming and Computer Science. In this lesson you will learn the definition of algorithm, how to represent them using natural language, pseudo-code and flow diagrams.
You will also see some examples.
The approximate time to complete this lesson is 15 minutes.
Transcription
In this lesson, we are going to talk about algorithms
Algorithms are one of the main concepts in computer science. That’s why we are starting this course on programming by studying algorithms.
It is widely used in many areas. For instance, in mathematics, physics and also, daily in our lives we are using algorithms.
It is a concept that is older than computers. It was created and used long before computers were created.
Let’s start with an informal definition of algorithm.
Table of Contents
- Algorithm definition
- Algorithm example
- How to choose the best algorithm
- Three ways of representing algorithms
- Recap
Algorithm definition
An algorithm is a finite sequence of well-defined steps. When the steps are executed in the given order, they solve a problem. The order of the steps is defined by the sequence of the steps. These steps can be executed without the knowledge of the problem that is being solved. This means that it doesn’t matter whether you know the problem that is being solved or not. You must be able to execute the algorithm.
The steps must be executed by a computer.
Not all algorithms are created to be executed by a computer. But, because we are in a course that is about programming, we are going to say that these steps must be executed by a computer. Therefore, every step needs to be clearly defined, in such a way that a computer can execute them.
Algorithm example
Let’s see the following example: calculate a perimeter of a square giving the length of one side.
The algorithm will be as follows
- assign the value of the side of the square to a variable named side. For now, we can think about a variable, informally, as a container where we put values so later, we can have access to those values.
- The second step of this algorithm will be to calculate the perimeter using the formula perimeter = 4* side
- The last step would be to print the value of the perimeter.
As you can see, this is it it is a very simple algorithm. This algorithm will solve our problem, which in this case is to calculate a perimeter of a square given the length of one side.
Let’s see a slight variation of the previous algorithm. In this case, all the steps are the same. The difference is that we are going to using a different formula to calculate the perimeter. Which is equivalent (obviously), but it is not the same formula.
Here we are summing the side’s length four times (perimeter = side + side + side + side), instead of multiplying the length of the side by 4 (perimeter = 4* side).
These are considered two different algorithms, although both will give you the same result.
Usually, several algorithms solve the same problem.
Then, which algorithm should we choose?
How to choose the best algorithm
Well, it depends, it will always depend.
The algorithms are compared according to time complexity and space complexity.
These two are the criteria that one must follow when we want to choose one algorithm among several others.
Time complexity refers to the amount of time that the algorithm needs to be executed and give the result.
Space complexity refers to the amount of memory that the algorithm needs to be executed and give the expected result.
All algorithms have certain characteristics that are very important. So anytime we design an algorithm, we need to consider these characteristics.
The first thing we should always consider is that an algorithm solves a problem. We don’t have an algorithm unless that specific algorithm solves a specific problem.
The steps that we use as part of the algorithm have an order. That order is the order that everyone (including the computer) must follow to solve the specific problem
Algorithms have a finite sequence of well-defined steps. It means they have a finite number of steps and, each of those steps must be executed in a finite time. Also, the execution of the whole algorithm will finish after a finite time.
The steps must be clear very clear and unambiguous. So, the steps must be well defined, everyone will understand what is required to execute each step.
For example, you cannot have a step that states: calculate a perimeter. You must specify how to calculate it, what is the formula, what value you are multiplying. So, for someone that doesn’t know what is a square and what the perimeter means, the person (or the computer) still can execute the algorithm and get the result.
Three ways of representing algorithms
We can represent an algorithm in three basic ways, using:
- natural language. Is the same way that we speak so we write an algorithm the same way that we speak we will see some examples.
- pseudo-code. It is similar to a programming language, but is not as formal as a programming language, therefore you can use symbols that you prefer to use. You can also use your own structures if they are like the ones that are used in a programming language.
- flow diagrams. For the flow diagrams, we are going to use certain symbols to represent the structures that we need to use in an algorithm. For instance, we are going to have a symbol to represent the start and the end of the algorithm. We are going to have another symbol to represent calculations, another one to represent input or outputs and, another symbol to represent decisions. We are going to see some examples later how we can use decisions in algorithms
Example
Let’s see the following example.
As you can see, on the left side we have an algorithm expressed in natural language. The first step is to assign the value of the side to a variable named side. The second step is to calculate the perimeter (perimeter = 4* side) and the last step is to print the value of the variable perimeter.
If we write that algorithm using pseudo-code, it will look like what you see in the middle of the picture. The first step is “read side”. The second step will be the formula. In this case, we are just using the variable names. We don’t have to write “calculate”. The last step is to print the perimeter.
Lastly, check out the flow diagram on the right side of the picture. Here we have the start symbol, then we read the side value using the symbol for input or output. After that, we calculate the perimeter using the figure (rectangle) for that purpose. Then, we have the symbol for output which in this case is to print perimeter. Finally, we have the symbol that represents the end of the algorithm.
Recap
In this lesson, we studied a definition of algorithm. Three main ways that we can use to represent an algorithm which are: natural language, pseudo-code, and flow diagrams.
You must know that we don’t use natural language to represent algorithms in programming. We mainly use pseudo-code. Flow diagrams are not used too much, but they help you understand how the flow occurs in an algorithm.
If you like this lesson leave a comment below.