What is an Algorithm?

Anubhav Choudhary
Jun 21, 2020 · 3 min read

As per the formal definition, “an algorithm is a finite sequence of instructions to solve some problem”.

To understand the above definition, we should first understand the meaning of the word “problem” used above. A problem is anything the you’re trying to solve computationally, it can be as simple as

  • Finding the maximum of two numbers
  • Arranging elements in ascending or descending order.
  • Finding the shortest path in a map or a graph.
  • Moving a drone from one point to another.
  • Suggesting the correct spelling for incorrect word
  • and so on.

Now, let’s pick one problem and then try to understand the definition of the algorithm.

Problem: Find the max of given three numbers

If we have to provide an algorithm for the above problem, we need a way to represent it. Thus, we can use “pseudocode” to represent the algorithm. Pseudocode is an informal way to describe an algorithm, we use simple English to list each step instead of any programming language-specific statements. So the following can be the pseudocode.

1 read n1, n2, n3
2 compare n1 and n2
3 store maximum from the last step as M
4 compare M and n3
5 store maximum from the last step as M
6 print M

As you can see that the above pseudocode has only 6 instructions by which computer can solve some problem. It is also matching with the definition of the algorithm which says, it must have a finite sequence of instructions and it must also solve some problem.

Can a problem be solved by multiple different algorithms?

Now, consider a scenario where we have a “Problem X” and there are three known algorithms, say Algorithm 1, Algorithm 2, Algorithm 3 to solve it. If we have to find the best algorithm, one approach that we can take is to find the execution time and the memory used by the algorithm for the same input and the system. Let assume, we got the following data for each algorithm :

  • Algorithm 1 : execution time= 5.0 seconds, memory = 1024 MB
  • Algorithm 2 : execution time = 6.0 seconds, memory = 756 MB
  • Algorithm 3 : execution time = 7.0 seconds, memory = 128 MB

By seeing the execution time, we might say that Algorithm 1 is better but if we have to solve the same problem on a device which has limited memory then Algorithm 3 will become more preferable. For the medium-sized device (in terms of memory), Algorithm 2 is best.

In the above scenario, we were able to select the right algorithm by using the data like execution time and memory used while assuming that the same data size and the same system is used for comparison. In reality, this method is not practical to compare two algorithms as it is difficult to get all this information for different combinations of input data-size and systems. Now, we are left with the following question.

If we have multiple algorithms for the same problem, how to compare and select the best?

In my next article, I will explain asymptotic notations in detail.
Thank you.

The Startup

Medium's largest active publication, followed by +775K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store