DEV Community

Cover image for Why we learn algorithms!?
Mohamed Sellami
Mohamed Sellami

Posted on

Why we learn algorithms!?

You may have wondered before why do I need to learn algorithms? You can solve problems without learning any algorithms, so why do you need to learn them!...Let's find out what algorithms are and why they are important!

First, let's talk about the meaning of algorithms. Algorithm is a word that refers to a well-defined set of instructions that can be followed to perform a particular task. Algorithm can be found in many fields, we can think of a recipe for preparing a particular food as an algorithm, it is a set of instructions that can be followed to prepare a food.

But the algorithms designed for a computer to execute them are different from the algorithms designed for humans, Humans can read between the lines or understand something a little mysterious. But computers can't do that, so algorithms designed for computers must be correct, precise, and unambiguous. A small mistake in the algorithm or ambiguity in a particular instruction can lead to unexpected results.

Algorithm consists of 3 basic structures:

  • Sequence: An algorithm contains a sequential set of instructions.
  • Conditions: Sometimes the instructions are not enough on their own so that we need a condition. If the condition is true, the computer executes some instructions, and if it is not, the computer executes other instructions. This is known as decision making.
  • Repetition: Sometimes we need to repeat some instructions several times until a certain condition is met.

For example, this is a simple algorithm to sum all numbers from 0 to n (n is a positive integer):

  1. set result to 0
  2. set count to 0
  3. if count is less than n
    • add one to count
    • add count to result
    • repeat step 3
  4. print result algorithm visualization

Now that we know what an algorithm means, let's answer our question why learn it!?

Algorithm is a way of thinking, it's teach how to solve problems and how to think like a computer. This is what we need to learn in programming, we need to learn how to think and how to solve problems and not just how to write code how to use languages and other technologies, in the end, languages ​​and technologies are just tools for implementing ideas and solutions.

To understand better why algorithms are important, let's think of algorithms like game play strategies.
Liam and Olivia compete in chess, but they both don't know how to play chess, so they are taught the rules of the game and how to move the pieces. But Olivia has studied some strategies for playing and Liam just learned the rules and how to move the pieces.
Liam and Olivia and chess
Now who do you think has the most chance of winning?

Well, Olivia would be much more likely to win than Liam. Because Olivia has learned how to think, not just how to move pieces.

It's almost the same in programming, developers who have learned algorithms will solve problems faster than developers who have only learned to use technologies. Maybe if you are dealing with small programs and small data you will not feel the importance of algorithms, but if you start dealing with complex and large programs and large data, algorithms will inevitably become very important.

Top comments (3)

betascribbles profile image
Firdaus H. Salim

I get your point really well. I started programming several months ago. It was easy to solve simple problems using just the fundamental concepts of a programming language. Once the projects/tasks get harder, it becomes tricky. I made a good choice to read the book "Programming Logic and Design, Comprehensive -by Joyce Farrell". From it I learned to think algorithmically and do my tasks easily, not a struggle anymore.
The ebook can be found here for free

mohamedsellami profile image
Mohamed Sellami

Me too, Since I started learning algorithms, I have become much better at solving problems

Thank you for sharing this book, looks very interesting!

jcubic profile image
Jakub T. Jankiewicz

I think that there are real misunderstanding, algorithms are base of programming, you can't write any program without know how to write algorithms. But I'm not talking about algorithms as written in books and touch as schools like sorting, or finding something in linked list. Any kind of program is created from algorithms. And you don't need to learn those classical examples of algorithms to be good with them.