Types of algorithms and examples

Programming is recording something using someone else’s unfamiliar language. With the development of this field of knowledge, the developers went even further and learned how to write “something” without even understanding how it sounds in Russian. Beginners learn to write code at once in C ++ or php using many libraries, and they don’t even really understand how what they create sounds in their native language. Algorithmization is engaged in explaining and bringing to understand this “something”.


Most examples of algorithms in computer science, even in universities, are studied at a mediocre level. A common practice is to write endlessly more and more complex code. Attempts by inexperienced programmers to start writing programs in the programming language right away can be compared to the work of a journalist who, barely having mastered the basics of a foreign language, writes an article for a magazine. You can avoid this problem if you start recording your work first in your own language, edit it, check for errors and finally translate it into the required language.

The advantage of this approach is mainly that the developer will be engaged in translation only 25% of the time, while writing a program in a new language, he will spend 100% on working with an unfamiliar language. At the same time he will be in cramped conditions and will not be able to conduct a good check for errors and refinement of the project.

Algorithmization helps in project implementation on a computer to describe the solution process in a native and understandable language in the form of a diagram of interconnected algorithms, analyze ideas and get the most high-quality and thoughtful code that will be more resistant to errors and work more efficiently.

Concept of algorithm

The computer does not know how to solve problems; it can only perform simple actions in the specified order. "How is the calculator?" - you ask. It is also the fruit of the work of programmers who created a program that uses certain algorithms to obtain the necessary results. Consider an abstract situation. What should be done if you are asked to find the roots of the square trinomial of a person who is not familiar with the methods of solving equations?

Obviously, he needs to be trained to solve quadratic equations. This happens as follows:

  1. Choose a solution.
  2. Examine all the details of the selected method.
  3. Explain the first two points to the future artist in a language he understands.

Then it will be possible to give the contractor tasks for solving a quadratic equation. And if the first two steps are simple and clear - all solutions are described in the relevant literature, then the third step is difficult.

How can you ensure that the ideas used in solving the problem will be perceived by the performer as you understand it? Here we come close to the concept of the algorithm. Practice shows that to correctly explain something to someone, the following steps must be followed:

  • determine the source data (variable and coefficients of the quadratic equation);
  • split the decision process into uniquely known components for the contractor (formulas for the discriminant and finding the roots);
  • specify the order of the steps (first calculate the discriminant, then the roots);
  • establish a condition under which the solution is considered complete (check the found roots, substituting them into the equation for the place of the variables);
  • indicate exactly how the result of the solution should be (roots belong to the set of real numbers).

The described set of steps in a general sense is an algorithm. Thus, the algorithm can be understood as a method of solving the problem, recorded using certain rules that allow you to provide an unambiguous understanding of the actions performed and their order. Below will be discussed in more detail the algorithms and examples of problems.

The main properties of the algorithm

Discreteness The process of solving a problem always consists of strictly separated from each other actions, called steps that have a specific order of execution.

Certainty Each step must be clear and unambiguous, both in meaning and in the key of the action to be taken.

Performance. The algorithm should give the result. In this case, the number of steps may be in the thousands or millions, but they must always lead to a result.

Mass. Any algorithm developed to solve a problem should be applicable to all problems of this type for all valid input data.

Computer features

To properly create algorithms for computers, it is important to understand their capabilities. Consider first the quantities with which the computer operates. In general, they can be divided into numeric and text, fixed and variable.

Constant numbers are all numbers: 3.15, 100, 105, their peculiarity is invariance throughout the program. Variables change their value in the course of code execution and are denoted, as a rule, by the letters: x, y, max, min, etc.

Text variables, like numeric variables, are constant or variable. In the first case, it is just the text: "good", "a and b", etc. In the second, the symbol is the same symbol as numeric variables: name, city, etc. The difference between them lies mainly in the computer's allocated memory under the storage of such a variable.

Operations that a computer is able to perform:

  1. Read data from input devices (keyboard, mouse, files).
  2. Calculation of values ​​using mathematical functions: addition, subtraction, sin, cos, ln, etc. - each programming language has its own set of built-in functions.
  3. Data output (on screen, on paper, to the network interface).
  4. The transition between the stages of the program.
  5. Comparison of two quantities (more, less, equal).

These are the basic operations that can be performed by most programming languages.

Ways to describe algorithms

Verbal. This is the easiest way. An example is the recipe. The use of simple mathematical formulas is allowed.

Graphic. Description using schemes. This is a special way to write algorithms using a kind of generally accepted algorithmic language - figures and blocks that have a specific meaning: a rectangle is a simple action, an oblique parallelogram is input / output, a rhombus is a condition, etc.

Algorithm for finding the maximum of three numbers

The use of algorithmic language. Similar to graphic, this is also a special way to write an algorithm. There are many algorithmic languages. Their rules are not strict, otherwise it would be a programming language. Consider an example of a payroll algorithm, depending on the length of service, recorded using an algorithmic language.

alg salary (int ST, real ZP) arg st ZP cut Start if ST <5 then zp = 150 otherwise if ST <= 15 then ZP = 180 otherwise, ZP = 180 + (ST - 15) * 10 end

Algorithmic language can be called a more rigorous form of writing than verbal.A limited set of words and their constructions are used, as well as indentation. The downside of the verbal form and algorithmic language is the deteriorating visibility of the algorithm with increasing its size. Therefore, these methods can only be used to convey the meaning of small algorithms.

Types of algorithms

There is a huge variety of algorithms designed to solve various problems. For example, any textbook of higher mathematics contains hundreds of algorithms: solving a system of linear equations, finding extrema of a function, calculating an integral, etc. However, upon a detailed examination of their structure, it turns out that all algorithms can be divided into several types. Consider these types of algorithms with examples.

  • linear (calculation of the result of addition or multiplication, the exchange of values ​​of several variables);
  • branching out (determining the largest of several numbers);
  • cyclic (array sorting, factorial calculation).

These are basic types. It is also worth noting that in a number of literature a fourth type is also highlighted - recursive. But it does not have a special designation in the schematic notation and is implemented through basic ones.

Example of branching algorithms

More details about each calculation algorithm with examples will be described below.

Principles of algorithmization

  1. Identify the source data.
  2. Choose a solution.
  3. Split the selected method into steps based on the capabilities of the computer (programming language).
  4. Run the algorithm in the form of a scheme, defining a clear order of steps.
  5. Displays the results of calculations.
  6. Mark the transition to the output circuit.

Algorithm debugging

A person makes mistakes, and this is a fact. The main parameter of any algorithm should be the correctness of its work. Debugging is the process of identifying and correcting errors of an algorithm. To do this, take a certain set of source data, called test. They are, as a rule, all kinds of source data. For example, if a number is inputted, the algorithm should be checked for correct operation, taking into account: positive, negative, integer and real numbers, zero values, etc.

The main tool for checking the accuracy of the algorithm remains the human brain. Of course, it is acceptable to use other computer tools to automate the verification, but one way or another, a person is involved in preparing tests and analyzing the results. In this case, the question arises, why do we need an algorithm if a person performs everything himself? Then, that the main task of the algorithm is the multiple solution of a certain type of problems.

Linear algorithms

A linear algorithm is one in which the steps follow one after another. Any algorithm that does not contain branches and cycles is linear. Consider an example of an algorithm that solves the following problem: a wolf and a hare sit in two cages, you need to swap them.

An example of a linear algorithm

The key to solving this problem is the additional cell temp, which should be used to swap animals.

Branching algorithms

As the name implies, the algorithm has several branches. The essence of the work lies in the choice of one of the possible variants of the computational process, depending on any conditions. A schematic branching is represented by a diamond-shaped block, inside which the condition is indicated, and on the sides of it are the branches of choice depending on whether the condition is true or false. The branching algorithm and examples of its application can be found everywhere. In programming, this is a typical if-else construct, which is in almost any language.

Branching algorithm

We give an example of an algorithm for solving the problem of finding the largest among the three numbers.

An example of a branching algorithm

Cyclic algorithm

Is cyclical algorithmin which there is a multiple repetition of the same steps, in which only the value of a specific variable over which calculations are made can change. Types of cyclic algorithms and an example will be discussed below, but for now we will list the main steps for building a cycle.

  1. Assigning the initial value of variables. Without the fulfillment of this condition, the cycle will most likely not be able to work or will make mistakes.
  2. The unit calculating the results. This is the main body of the cycle.
  3. Check the end condition of the cyclic process If you forget to specify the condition under which to complete the cycle, the algorithm will run indefinitely.
  4. Change variables This block takes effect after checking the condition of the termination, if it was false. If we forget about this block, the cycle will always perform one action and will never end. Therefore, it is important that the variables undergo any changes at each iteration of the loop.

There are several types of cyclic algorithms: with postcondition, precondition and parameter.

Types of cyclic algorithms

We construct a cyclic algorithm using the example of finding the factorial of N.

Algorithm for finding factorial

Other types of algorithms

There are a number of algorithms that differ in classification or origin.

  • Mechanical algorithms. For example, the operation of an internal combustion engine or an assembly line.
  • Probabilistic algorithms. Their work is based on probability theory and mathematical statistics.
  • Heuristic algorithms. Use practical considerations in their work, without a rigorous mathematical justification.

Related news

Types of algorithms and examples image, picture, imagery

Types of algorithms and examples 25

Types of algorithms and examples 75

Types of algorithms and examples 30

Types of algorithms and examples 44

Types of algorithms and examples 15

Types of algorithms and examples 27

Types of algorithms and examples 32

Types of algorithms and examples 16