What is an algorithm? Definition, structure and examples

An algorithm is a detailed step-by-step set of instructions aimed at solving a problem.

Algorithms are the beating heart of modern computing. Every time you perform a search on the Internet, send an email, watch an online video or shop on a website, they are working behind the scenes to make it all possible. These systems enable computers to solve problems, make decisions, and perform tasks efficiently and effectively.

In this article, you can discover one of the main concepts of computer science and machine learning, along with clear definitions, explanations, and 3 real-life examples.

So, don’t waste any time and jump straight to the article.

What an algorithm is (definition)

As said before, an algorithm is a detailed step-by-step set of instructions aimed at solving a problem.

Algorithms’ main elements

An algorithm is composed of control structures, structures that manage the execution of an algorithm.

There are three main control structures:

Sequence

A set of instructions executed one after the other, in succession.

Selection

A selection allows the machine to execute different instructions depending on a condition (Es. if X = 3, execute Y, if not execute Z).

Iteration (cycles and loops)

An iteration allows the machine to execute a set of instructions multiple times.

There are two types of iteration:

For loop

The for loop is used to iterate over a sequence of elements.

While loop

The while loop is used to iterate as long as a certain condition is true.

The 3 steps of an algorithm

1. Input

The input data is our starting point. These values could be any form of information, such as numbers, text, images, or other variables, depending on the problem the program is designed to solve.

2. Processing

The computer processes the input data by following the instructions.

The latter are expressed to machines through natural language, programming languages, pseudocode, and flowcharts (check below).

3. Output

The data is modified according to the instructions in our output, which is the solution to our problem.

A simple real-life example can be useful in explaining how algorithms work. Imagine we have to prepare a recipe. The ingredients are our input data, the instructions are the algorithm and the prepared food is the output.

Algorithm representation

Problem statement

We receive as input two test marks between 1 and 10.
If the mean score is over 6, the trimester is passed, otherwise it’s failed.
We need an output “passed” or “failed”.

Natural language

Through natural language, algorithms can be expressed with simple lists.

  1. Read the 2 marks.
  2. The mean is calculated by summing the input values and dividing them by 2.
  3. If the mean is 6 or bigger, display “passed”. Otherwise display “failed”.

Flowchart

A flowchart is a diagram representing an algorithm using blocks of different geometric shapes for each control structure.

Pseudocode

Pseudocode is a textual representation of an algorithm that uses plain language and a mixture of natural language and programming language without being tied to a specific syntax.

Pseudocode is not meant to be executed on a computer but serves as a way to plan and design algorithms before implementation in a specific programming language.

start
	read mark_1

	read mark_2

	mean = (mark_1 + mark_2) / 2

	If mean >= 6 do:
		display “passed”
	else:
		display “failed”
	endIf
end

Programming languages

Programming languages are textual languages to represent algorithms and make them interpretable and executable for a machine. They are characterized by their syntax and semantics.

mark_1 = float(input("Insert mark 1: "))

mark_2 = float(input("Insert mark 2: "))

mean = (mark_1 + mark_2) / 2

if mean >= 6:
    print("passed")
else:
    print("failed")

Example of algorithms

Rubik’s cube resolution

Algorithms are a fundamental part of Rubik’s cube resolution. In fact, in each of the many approaches, they are necessary to complete the puzzle.

In the basic method to solve the 3×3 Rubik’s cube (clearly explained on this website) algorithms are a succession of moves you must do depending on the puzzle situation.

For example, after completing the first layer of the cube, we have to use 2 algorithms (Right and Left) to complete the second layer.

Pythagorean theorem

  1. Receive as input the length of the 2 catheters
  2. Calculate the hypotenuse using the formula:
\[i = \sqrt{a^2 + b^2}\]

Social media recommendations and feeds

Social media applications like YouTube, TikTok, and Instagram have sophisticated algorithms at the heart of their functioning.

These algorithms, based on the user’s actions on the platform can derive a profile of the person, which contains information about his or her characteristics, interests, needs, and routines. The latter is used to recommend suitable content to keep the user on the platform as long as possible.

Algorithms in machine learning

In machine learning, algorithms refer to the set of instructions to build a machine learning model.

Models are at the core of machine learning and they can recognize patterns, identify trends, classify data, and make decisions based on input information.

Share the knowledge