To create any programs you need basic algorithmic constructs. Following is the easiest way to solve problems. It can be used, for example, to work with similar examples. There are other types: branching and looping. They will be described in this article. But first you need to understand what constitutes the algorithm as a whole.
Algorithm
The word "algorithm" comes from the Latin algoritmi. What does it mean? The authentic word came from the name of a mathematician, whose activity fell on the 9th century. Thanks to the treatise of al-Khwarizmi, mankind was able to get acquainted with the main type of algorithmic construction and, in general, with the general concept.
Earlier, the spelling form of the word was adopted - "algorithm". Now it is used only in some cases.
An algorithm is a process that means a change in the source data that occurs in the form of discrete steps. With this concept, every person encounters in life, no matter who he is. Algorithms can be called cooking tea or food, multiplication or addition, solving equations, etc. All household appliances, whose work process is automated, operates due to the clear steps prescribed in the processor memory. Such algorithms are called household. There are other types. Consider them.
Types of Algorithms
The basic algorithmic constructions are divided into several types, which will be discussed in this subclause. What are they like?
- Informational. Such algorithms work with a large amount of data, but the volume of the processing process is small in length and uncomplicated.
- Managers. The operation of such algorithms is associated with information that is provided from a particular source. After receiving it, special signals are sent that guarantee the operation of the devices.
- Computational. Unlike informational algorithms, the described ones work with small amounts of data, but produce a large work process.
In essence, the algorithm is an instruction that is accurate to the smallest detail. However, not all such data can be called the described concept. To understand whether an algorithm is an instruction or not, it should be checked for certain properties.
Algorithm Properties
All basic algorithmic constructs must have actions that “obey” them. Let's consider this question in more detail.
If you fully track the operation of the algorithms and their properties, you can see that it is not necessary to understand their components, it is quite clear to plan. The correct result will be obtained even if you simply mechanically adhere to the necessary actions. From this we can conclude that due to the lack of sense in the awareness of actions, the algorithm is quite realistic to give to the implementation of the computer. In other words, for automated devices this process is necessary.
What properties should the basic algorithmic constructions have for the most accurate operation?
- Clearness. Each command should be as clear as possible to the object being executed. It seems to be nothing easier than, for example, drawing a point in the center, no, but until a command is written that allows you to perform an action, you will not succeed.
- Performance. What does this property mean? Obligatory receipt of the result. The algorithm cannot but lead to some kind of answer. Due to the error, you can get not the result that was desired, but still it will be. Moreover, the answer must be obtained in a certain number of steps.
- Massiveness. Any algorithm should be applicable to some class of problems. Between themselves, they may differ in the source data.
- Certainty. Each action should have only one meaning and not give the possibility for a derivative decryption. Ideally, no matter how much the program starts, the result should always be the same.
- Discreteness. Algorithm - sequential steps. Each step is a team, you cannot skip or add new ones.
- Correctness. Any algorithm that applies to some kind of task should be right for everyone. In programming, problems often arise not in writing steps, which often do not require much time, but in performing them for various kinds of questions. Therefore, an important step will be debugging the algorithm. The basic algorithmic constructs can help in this, the repetition of which will achieve better results.
Description of Algorithms
If we talk about ways to write algorithms, then the following should be highlighted:
- Verbal. In other words, in a language that is convenient for the component to communicate.
- Tabular. By the logic of things, the algorithm is written into tables and, as a rule, is used as an auxiliary element.
- Formular verbal. The verbal way of explanation is taken as the basis, but mathematical formulas or symbols are also written in such actions.
- Graphic. Such an algorithm is written in a special language of flowcharts.
The last point should be clarified. What is a flowchart? This is a linear or non-linear algorithm, the steps of which are recorded using special blocks. They have their own configuration, purpose and function. In the case of such a description, the algorithm is written in block diagrams that are interconnected by lines. In them, it is necessary to additionally write down one or another action (step).
Algorithmic Constructions
Some argue that the algorithms do not have 3 types, but 4. Basic algorithmic constructions: linear, branching, cyclic. What is the reason for such a fallacy is not clear. However, for a simple solution to complex problems, computers use the algorithms of these three fairly large groups. Consider them.
- Linear. Such a computational process received this name due to the fact that all actions are performed in a linear sequence, with each step being performed no more than once. If we consider the task scheme, then the blocks in it are placed one below the other, depending on the sequence number of the execution. Linear algorithms work in such a way that the direction and meaning of actions do not change from the source data. This method of solution is suitable for calculating the sum or difference, the area of a figure or its perimeter, etc. It is he who is the main type of algorithmic construction.
- Branching. This computational process implies the presence of a logical expression (hereinafter referred to as the LP) and the choice of a condition (the “false” and “true” branches). In each case, only one of two or more teams is implemented. There are no tasks and cannot be, in which other options will be fulfilled. If the algorithm has two branches, it is simple; if more than two, it is complex. Moreover, the latter process is easily presented at the expense of the first. The main type of algorithmic construction is both the first paragraph and the second. The following view is also included in this list.
- Cyclical. In such an algorithm, there will certainly be an element that is repeated many times, while using different source data. In other words, such a process is called a cycle.
It should be noted that all the basic algorithmic constructions (following, branching, loop) are interconnected with each other, although they can be used separately.
Creating loops and their types
What is needed to create a loop?
- Cycle counter. This is a variable that sets the initial value, and when the action is repeated, it will change. It must be included in the algorithm. The basic algorithmic constructs of the cyclic type will not work without it.
- Change of the indicator of the above data before a new repetition of the cycle itself.
- Checking the condition so that the computer decides whether to cycle again or not.
Loops can be deterministic and iterative. The former are repetitions of actions with an already known number of repetitions. An iterative cycle is one that is repeated an undetermined number of times until a condition becomes true or false.
Basic algorithm
It is worth remembering that the basic algorithm does not apply to the basic algorithmic constructions. What is he like? This concept has not been found in modern literature for a long time, but this does not mean that it no longer exists at all. Considering that in solving problems several branches or repetitions can occur, we can distinguish the following conclusion. The basic algorithmic constructions (linear, branching, cyclic) are basic. In fact, they represent the “structural unit” of each so-called instruction.
Linear Algorithms
As already clear from the above, the algorithms are linear and non-linear. Consider the first option. Why is he called that? Everything is extremely simple. The fact is that all actions that are reproduced in the algorithm have a clearly sequential execution, all steps are performed strictly one after another. As a rule, such tasks are small and have a low level of complexity.
An example of a linear algorithm is the process of making tea:
- Pour water into the kettle.
- Put the kettle on the stove to boil.
- Take a cup.
- Pour tea into a cup.
- Add sugar.
- After boiling, pour boiling water into a cup.
- Take a spoon.
- Stir the sugar.
Programming the basic algorithmic constructions is a rather difficult task, but if we are talking about linear algorithms, it is often very easy to implement them.
Branching algorithms
How to understand that the algorithm is branching? It is enough to make sure that there is a choice of two or more options, depending on whether the condition is fulfilled or not fulfilled. Each path is called a branch.
The main feature of a branching algorithm is the existence of a conditional transition. It occurs while checking the expression for true or false.
Typically, logical expressions are represented by less than, greater than, less than or equal to, greater than or equal to, equal to, not equal signs. Sometimes there are options where the condition is interconnected using the and (and) and or (or) commands.
An example of such an algorithm can be the solution of the following problem: if the expression ((x + 3) / 1) is a positive number, then display the result on the screen, if it is negative, inform the user about the error.
It’s quite simple to use the basic algorithmic constructs in practice. Branching is one of the most common solution methods.
Deterministic or Counter Cycle
A cycle with a counter is a cycle that includes a variable that changes a value with a certain step. The step is set by the user or prescribed by the programmer while writing the software. Most languages for this loop use the for statement.
In order for the program to display two lines 4 times:
- "How are you?"
- "Well thank you!"
- "How are you?"
- "Well thank you!"
It is necessary to create a deterministic cycle. What does it look like? We use the Pascal language for a better perception of the design.
1. For i: = 1 to 2 do:
- i is the loop counter, it is he who determines the number of repetitions in the loop.
2. Begin (operator brackets are opened so that both phrases are the body of the loop and are repeated together.)
3. Writeln ('How are you?'):
- the word writeln means the output of a phrase in single quotes.
4. Writeln ('Okay, thanks').
5. End.
6.i: = i + 1.
As you can see, it’s quite easy and even interesting to use the basic algorithmic constructs. The basic algorithms are really widely known, without them it is impossible to write programs.
Postcondition Loop
A postcondition loop can repeat an indefinite number of actions without inserting operator brackets or compound words in them. It will be executed at least once. The loop runs while the condition is false. It stops when the indicators become correct. The algorithm is built on this. The basic algorithmic constructions of this type work precisely at this pace.
To implement this loop, the Repeat A until B construction is needed. Literally, it translates as "repeat actions until the condition is false." Accordingly, through A the process of repetition is expressed, through B - data, which as a result should take the correct value.
Preconditioned Cycle
A cycle with a postcondition is constructed in such a way that it is executed at least once in any case. However, there are cases when a cycle is necessary in the case of a particular condition, and in its absence, repetitions should not be carried out. Otherwise, the result will be incorrect. In this case, a cycle with a precondition is used. To create it, the “while A do B” construct is needed. The first command literally translates as “bye”. A is the condition, and B is the action that will be repeated. The whole construction means: "as long as the condition is true, perform the action."
All basic algorithmic constructions work only in certain cases. What are they in a cycle with a precondition? If it is necessary that more than one action is repeated, but several at once, then it is worth using either compound operators or special brackets. A cycle may well fail if the condition is not true when it enters it. Accordingly, the actions will be repeated if it is correct.
Helper Algorithm
The auxiliary algorithm is used in other processes by indicating only its name. It does not apply to the basic algorithmic constructions. In programming languages, this process is called a subroutine. To facilitate working with the code and subsequently easier task solving, each action is combined into one block, which is an auxiliary algorithm. Each of them can be given its own name, which subsequently allows you to repeatedly contact him.