In this article, let's understand what is an algorithm and how can we relate an algorithm with real world example and its characteristics and finally why should we learn algorithms.


What is an algorithm?

In the most general sense an algorithm is a set of well-defined instructions to solve a particular problem. 



The simplest real world analogy is that of following a recipe.

Recipe analogy

You have a set of ingredients to cook with to prepare a dish and then you carefully follow the recipe and execute the steps one by one in a sequence. The end result is a tasty dish.

Recipe analogy


If you're preparing instant noodles, you should have a cup of noodles, taste maker and water as the ingredients.

For the recipe,  you have steps like boil water, pour water into the cup, add taste maker and close the lid and wait for 4 minutes. The output is a cup of tasty noodles that you can enjoy.

Let's extend this to computer programming

Programming

Consider an algorithm to add two numbers, in this case we refer to the ingredients as inputs, the recipe as algorithm and the tasty dish is the output.

programming


In our scenario, inputs will be two numbers a and b, the algorithm steps include adding the numbers using the plus operator and returning that value. The output is the sum of the two numbers.

So we have our set of well-defined instructions to solve a particular problem, which in our case is adding two numbers.

Hopefully, you now understand what an algorithm is, next let's take a look at a few characteristics of an algorithm

Characteristics

1. An algorithm should have defined inputs and outputs (in our example, we have two numbers as

inputs and their sum as the output. There is no confusion about that)

  • Input − An algorithm should have 0 or more well-defined inputs.
  • Output − An algorithm should have 1 or more well-defined outputs, and should match the desired output.

2. Each step in an algorithm should be clear and unambiguous (in our example, we have two steps

add the numbers using the plus operator and return the sum, if you ask me that is as clear as it can be)

3. Finally, an algorithm should be language independent (it should be written in such a way that it can be used with different programming languages)

You can see that is the case with our example as well, it is just a sequence of instructions that can be implemented in any language.

Although I had mentioned, we would be using javascript to write our code. The algorithm itself is by no means related to javascript.

Now why should you learn about algorithms

Why Algorithms?

1. Well as a developer you're going to come across problems that you need to solve

2. When you learn about algorithms, you're basically learning different techniques to efficiently solve those problems and this last bit is important to keep in mind

3. One problem can be solved in many ways using different algorithms and every algorithm comes with its own trade-offs when it comes to performance, which brings the question how do we measure an algorithm



well let's discuss that in the article. Here is the reference to learn more about algorithms