How to Write and Analyze an Algorithm

Learn how to write and analyze an algorithm, as well as the criteria by which we can analyze an algorithm.

Picture of Nsikak Imoh, author of Macsika Blog
White background image with the text  How to Write and Analyze an Algorithm
White background image with the text How to Write and Analyze an Algorithm

Table of Content

Although we mentioned that we would be implementing our Algorithm using Python, the algorithm itself is by no means dependent on Python.

How to Write an Algorithm

Here are some guidelines to follow when writing an algorithm to be language-independent:

  • Variables and function parameters should not be written with a data type.
  • Enclose code blocks using curly braces {...} or a Begin...End statement.
  • Assignment operators can be written as =, :=, or .
  • You can use a semicolon ; to terminate statements or not.

Here are some examples of an algorithm that adds two numbers:

Algo add(a, b)
{
    c = a + b;
    return c;
}

Algo add(a, b)
Begin
    c := a + b;
    return c;
End

How to Analyze an Algorithm

There are several criteria with which an algorithm can be analyzed. The criteria to analyze an algorithm depends on the type of software project or program being built.

However, the two major criteria that are commonly used are time and space.

1. Time

The time taken to analyze an algorithm is important regardless of the method of measurement, whether manually using pen and paper or by a computer program.

We need to check if the algorithm is lengthy and time-consuming or fast. The result of that analysis is a time function.

When you write an algorithm, you should aim for it to be time-efficient and fast.

2. Space

This is the amount of memory an algorithm will consume when it's translated into a program to run on a computer.

Analyzing the algorithm for space is important to determine if the algorithm will consume more or less memory. The result of that analysis is a space function.

When you write an algorithm, you should aim for it to be memory-efficient and consume less space.

Depending on your project requirements, here are other criteria to analyze an algorithm:

3. Data transfer and consumption

You may want to analyze an algorithm to check the amount of data consumed as it gets transmitted over the network or cloud service.

In this day and age, with the way applications are designed, it may be necessary to check if an algorithm is transmitting a large amount of data and how to optimize it or probably compress the content being transmitted.

4. Power Consumption

Having applications and programs that consume less power is becoming increasingly demanding as device manufacturers aim to make their devices have long-lasting power.

It may be necessary to analyze an algorithm for how much power it consumes whether it runs on a mobile device, tablet, or personal computer.

5. CPU Registers

Let's assume you are a systems engineer developing an algorithm for a device driver or system, it may be necessary to know the number of CPU registers the algorithm will utilize when it runs.

Example on How to Analyze an Algorithm

We will go into detail on how to analyze an algorithm using the language-independent format.

Algo add(a, b)
{
    c = a + b;
    return c;
}

Time analysis of an algorithm:

When checking the time taken for an algorithm to run, we take each significant statement as a single unit of time.

It is called a unit of time because we do not know what units the time will be measured in when translated into a program.

In the program above, there are two significant statements:

c = a + b; ----- 1 unit of time
return c; ------ 1 unit of time

They each take a unit of time as stated. This means that the combined time taken by the program is 2 units of time.

This is represented as a time function.

f(t) = 2

Space analysis of an algorithm

When checking the space consumed by an algorithm, we need to each variable or parameter that holds a value and count it in terms of words.

It is called word because we do not know what measurement system will be used for testing when translated to machine codes.

In the program above, there are three parameters and a variable.

a ----- 1 word
b ----- 1 word
c ----- 1 word

This means that the combined space taken by the program is 3 words.

This is represented as a space function.
f(s) = 3

Representing Constant Functions of Time and Space Analysis

When the value of a time or space analysis of an algorithm is 1 or 1 million, it is usually represented as just 1.

This means that the time and space analysis of the algorithm, above, will be:

f(t) = 1
f(s) = 1

Furthermore, the result of time and space analysis of the algorithm, are usually represented using the big O symbol, we will explain more on this in later tutorials.

Hence, we write our solution to be:

time = O(1)
space = O(1)

Wrap Off

In this lesson, we looked at how to write and analyze an algorithm, as well as the criteria by which we can analyze an algorithm.

If you learned from this tutorial, or it helped you in any way, please consider sharing and subscribing to our newsletter.

Please share this post and for more insightful posts on business, technology, engineering, history, and marketing, subscribe to our newsletter.

Next Tutorial — What is the Time and Space Complexity of an Algorithm?Get the Complete Code of Algorithms: Baby-Steps to Complete Mastery on Github.

Connect with me.

Need an engineer on your team to grease an idea, build a great product, grow a business or just sip tea and share a laugh?