DEV Community

Cover image for Algorithm
Valentiina VP
Valentiina VP

Posted on • Updated on

Algorithm

What is an algorithm?

In summary, we can define an algorithm as a set of instructions or rules defined, ordered and finite that allows solve a problem. In reality, the human being performs algorithms daily. But it happens, that being routine actions, our brain performs them automatically, almost without us being aware of them.

An example of this is, when in the morning we get up and prepare for breakfast. We do it automatically, but certain “instructions” are occurring in our brain that cause our body to move to perform these actions:

  • Start
    1. We open our eyes
    2. We get out of bed
    3. We put on sandals or house shoes.
    4. We go to the bathroom
    5. We brush our teeth
    6. We take a shower
    7. We get dressed
    8. We go to the kitchen
    9. We prepare breakfast.
  • End

In the same way it happens with a computer system. In this case, since the machines at this moment do not think for themselves, we will be their brain. We, through a programming language, will give her instructions, step by step and in a logical way, of what we want her to do. These instructions are what we call algorithm.

For example, if we want an algorithm that allows us to know if a number is even or odd, we can create this flow diagram where we can visualize it. We will always have a beginning and an end.

Alt Text

This algorithm will be written in a programming language using a code editing tool. The code undergoes a series of transformations to finally get the program that will be executed on a computer.

Execution

In the execution it is necessary that the program is stored in the internal and volatile memory of the computer, so the processor will read each order of the program, solving it and executing it.

If necessary, internal memory is accessed to manipulate variables, input, output, or storage peripherals. Then, it does the necessary calculations and solves the logical expressions, until the last instruction completes successfully or an error occurs.

This is an example of the algorithm encoded in the programming language Java (Left) and JavaScript (Right)

Alt Text

What the system returns to us, in the case of n being the number 6, is that 6 is an even number.

In conclusion, algorithms allow us to think logically about the order in which our programs will be developed and executed to obtain the best results and solve problems optimally.

Latest comments (1)

Collapse
 
feministclickback profile image
tina

Thank you, this is helpful!