So, What an algorithm really means?

When I started learning to code, the doubt I had was what is meant by algorithms, are data structures and algorithms the same and if not, then what's the relation between these two.

So this blog explains every doubt you could have while studying algorithms.

What is an algorithm?

Algorithm is just a set of instructions we use to solve a problem. I can explain it with a simple real-life example

Lets assume we have a problem here. The problem we have here is to find a word's definition from a paper dictionary . For example we want to find the word ‘doll’.

We know that the words in the dictionary are alphabetically ordered. So we can guess where the word can be found.

Firstly we take the middle of the dictionary. Are the words in the page starts with the letter 'd' . If yes we can find the word on that page or nearby pages.

If not we have to keep looking. For example we got the page with the letter 'k' . We know that ‘d’ can only be found before ‘k’, which is on the left side of the dictionary. So we can skip the right part after the middle.

So then there is only the left part. Now it's easier for us to find the word.

Then we take half of the left side. We got the letter ‘f’. Can 'd' be found on the left side or right side of ‘f’. Of course the left side. So we again skip the right.

This can be done till we find the word 'doll'

So above are the instructions you can use to find the word from a dictionary.

We can write it as an algorithm.

*1. Take the word from the user

  1. Take the middle of the dictionary
  2. If you found the word
  3. Return the definition
  4. If not check whether the word can be found on the left part or right part of the dictionary
  5. If it's in the left part, skip the right part
  6. If it's in the right part, skip the left part
  7. Repeat from step 3 to 7 until you find the word
  8. then stop*

So when we write algorithms, it doesn't have to be the exact code. We can write it in plain English and it should be applicable to every programming language.

There are some conditions for writing algorithms

  1. An algorithm should have an input
  2. It should be finite
  3. It should be clear
  4. It should have an output
  5. Should be easily doable.

That’s all. Now you know what an algorithm is. See you all in the next article.