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.
Table of Contents
What an algorithm is (definition)
As said before, an algorithm is a detailed step-by-step set of instructions aimed at solving a problem.
Usually in computer science and its subfields the word “algorithm” is also used as a synonym for a program or a computation process.
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.
How algorithms work
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
Natural language
Through natural language, algorithms can be expressed with simple lists.
- Choose what to cook
- Check if there are the ingredients in the fridge
- If yes, cook the dish, otherwise change what to cook (1.)
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.
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.
#previous algorithm in Python
def choose_dish():
...
def cook():
...
fridge = []
ingredients = []
while True:
choose_dish()
if ingredients in fridge:
cook()
break
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
Our input is the length of the catheters of a right triangle, and we want to calculate the hypotenuse length using the formula :
a2 + b2 = c2
- Square the 2 catheters (a, b)
- Sum the squared catheters to find the squared hypotenuse (c2)
- Calculate the square root of c2
Social media recommendations and feeds
Social media applications like YouTube, TikTok, and Instagram have sophisticated algorithms that are 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 in order 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.