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):
- set result to 0
- set count to 0
- if count is less than n
- add one to count
- add count to result
- repeat step 3
- print result
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.
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.