The sequence of steps we take to achieve the desired task is known as an algorithm. In programming, when we create and execute a plan by writing codes to solve a problem, those plans are nothing more than an algorithm. In this article, I will give you an introduction to algorithms in programming.
What is an Algorithm?
We perform so many activities in our daily life following a sequence of steps. For example, activities like riding a bicycle, using a computer or driving a car, all of these activities follow a sequence of steps. Let’s say we want to find the greatest common divisor of 45 and 54, so to find the GCD we need to follow these two steps:
- Find the numbers which can divide 45 and 54
- Divisors of 45: 1, 3, 5, 9, 15 and 45
- Divisors of 54: 1, 2, 3, 6, 9, 18, 27 and 54
- Then find the largest common numbers from the divisors of both the numbers
- Then GCD of 45 and 54 is 9
So this is what an algorithm means, we understand a problem and follow a sequence of steps to solve that problem. Algorithms have a defined start and end and always contain a finite number of steps. If we follow the steps of an algorithm accurately, we will get the desired result within a limited amount of time.
Why We Need An Algorithm in Programming?
During programming, a programmer writes a program to give instructions to the computer to solve certain problems. The computer then follows all commands as directed by a programmer. So it is very important to follow an algorithm which acts as a program roadmap to solve a particular problem.
Writing an algorithm is the first step in programming, and then following it as a roadmap while writing the code to solve a particular problem is the second step. If the algorithms we follow are correct, we can work on any complex task.
Below are some of the factors to identify good algorithms:
- Accuracy: The steps mentioned when creating algorithms should be stated precisely and well defined.
- Uniqueness: The results of each step should be uniquely identified and event-driven, meaning that the next step should depend on the input from the previous step.
- Finitude: the algorithm must stop at a finite number of steps in terms of parameters and iterations.
- Input and Output: Each algorithm should accept user input and produce output that can solve your problems.
There are two common ways among programmers to describe and understand the process of algorithms; a flowchart and pseudocode. It doesn’t matter if you have another method of framing and designing algorithms before writing any code. Hope you liked this article on what algorithms are in programming. Please feel free to ask your valuable questions in the comments section below.