An algorithm is a set of instructions for performing a specific task. We encounter algorithms in everyday life at every step, often without even realizing it.
In the morning, we brush our teeth, roughly following the scheme presented below:
- squeeze a small amount of paste onto the toothbrush,
- brush your teeth for 2 minutes,
- rinse your mouth with water a few times,
- wash the toothbrush under running water.
Later, we prepare our favorite scrambled eggs for breakfast, also implementing a recipe we know perfectly well:
- put some fat, e.g., butter, on the pan,
- light the stove burner, put the pan with the fat on the burner,
- crack 3 eggs onto the pan, discard the shells,
- add two pinches of salt,
- stirring, wait until the contents of the pan thicken,
- turn off the burner,
- place the contents of the pan on a plate and eat.
In a similar way, dozens of daily activities can be described, which means providing algorithms for their execution. Each of these instructions tells us how to transform a certain initial state of a given system into the desired final state (dirty teeth -> clean teeth, raw eggs -> scrambled eggs).
In mathematics and computer science, an algorithm is also the name for a set of instructions that tells us how to perform a certain task, to achieve a specific goal. At the same time, however, we formulate certain strictly defined requirements for this set of instructions, which must be met in order for it to be called an algorithm:
An algorithm is an ordered, finite sequence of unambiguously defined actions, the execution of which in a finite time leads to the solution of a task.
An example of a mathematical algorithm, finding the absolute value of a number
- Load x
- Is x <0?
- If YES: x: = - x (change the sign to positive)
- If NO: do nothing
- Write x (the absolute value of the read number)
Methods for Algorithm Representation
We can represent algorithms in various ways: descriptively, in numbered steps (which we've been doing so far), using instructions from a chosen programming language, or graphically, by applying generally accepted symbols. This last method is very popular. Below, we will present the symbols we'll use for algorithm notation:
The Start of the Algorithm. The symbol for the start of the algorithm appears exactly once. No flow line (arrow) comes into it, and only one flows out.
The End of the Algorithm. The symbol for the end of the algorithm, like the start symbol, must appear exactly once in the algorithm. No flow line (arrow) flows out of it, and at least one flow line comes into it.
The Input/Output block of the algorithm. It represents the action of introducing data into the algorithm (e.g., read: x) as well as the output of the received results and messages (e.g., write: y). The Input/Output block is associated with exactly one incoming flow line (arrow) and one outgoing flow line.
The Operational Block, contains the operations performed by the algorithm. All calculations executed during the implementation of the algorithm are contained within operational blocks. Each operational block has exactly one incoming flow line (arrow) and exactly one outgoing flow line.
The Decision Block, also known as the Conditional Block. A condition is placed inside the block, which can either be met or not met. If the condition is met (true), we exit the block via the path labeled "YES" (or "TRUE"), while if the condition is false, we proceed along the path indicated by the arrow labeled "NO" (or "FALSE"). One input flow line (arrow) comes into the decision block, and two output flow lines (arrows) exit ("YES" and "NO").
Now that we're familiar with the symbols used for the graphical representation of algorithms, we can draw the algorithm for finding the absolute value of a number, which was described earlier:
