What is an algorithm? Definition, types 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.

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).

There are three types of selection:

Single alternative selection

Double alternative selection

Multiple alternative selection

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.

  1. Choose what to cook
  2. Check if there are the ingredients in the fridge
  3. 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 code

This is a Python code that tells the computer to calculate
the hypotenuse’s length of a right triangle given the two catheters, using the Pythagorean theorem code

This is a clear example of an algorithm, where our input data are the two sides, the algorithm is the square root of the sum of the two squared catheters and the output is the hypotenuse.

import math

def CalculateHypotenuse(Catheter1, Catheter2):
     Hypotenuse = math.sqrt(Catheter1 ** 2 + Catheter2 ** 2)
     return Hypotenuse 

print(CalculateHypotenuse(3,4))

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 computational procedures and mathematical models used to enable computers to learn from data and make predictions or decisions without being explicitly programmed to do so.

Machine learning algorithms are at the core of building models and systems that can recognize patterns, identify trends, classify data, and make decisions based on input information.

Share the knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *