Algorithm is an important concept in programming and every programmers must be well versed in it to write efficient codes. According to Wikipedia Algorithm is defined as “a set of rules that precisely defines a sequence of operations”. Algorithm is basically a set of simplified rules or instructions that will help users to perform a task or operation in much efficient manner. Let’s see some analogy to understand this better.
To understand what is an Algorithm in programming we must know what Algorithm means in general. To do so consider an analogy where John’s mom ask John to buy some vegetables from the shop. As stated earlier Algorithm is a simplified instructions that breaks down a task in multiple steps that helps user to perform that ask. Here in order to buy the vegetables John must follow the below Algorithm
Step 1: Get the list of vegetables from mom
Step 2: Visit the shop
Step 3: Pick the vegetables from the list his mum provided
Step 4: Handover the money to vendor
Step 5: Come back home
If you observe the above steps, it’s clearly seen that the task of buying vegetables is broken down to small steps that helped John to complete the task successfully. This is what Algorithm is all about in general.
ALGORITHM IN PROGRAMMING:
In programming Algorithm provides step by step instructions to write code to perform the given task at hand. There are plenty of programming Algorithms and it ranges from simple to complex depends on the type of task it needs to perform. Let’s consider a simple task to understand this better. Consider you need to add the values of two variables A and B in your program. For that purpose you need to follow the below algorithm.
A + B
Step 1 : Read the value of Variable A
Step 2: Read the value of Variable B
Step 3: Add the values of A and B
Step 4: Display or store the result of addition A and B
The above steps ( algorithm ) breaks the task of adding two variables in two 4 sequential steps that provides logic for programmers to write their code to add the values of A and B. Programmers will use the logic given in the above steps and write their code accordingly. Above task is an easy one so Algorithm will be small and simple. However for complex tasks and operations Algorithms tend to be complex with many sequential steps of complex operations.
IMPORTANT THING TO REMEMBER:
As explained above Algorithms provide sequential steps that provides logic to programmers to write their code. So Algorithm is independent of programming languages you use. Be it C, C++, C#, Java or Python etc Algorithm always remain same since it only provides logic to write the code. It’s up to the programmer to implement the steps from Algorithm into their code to make it run efficiently.
TWO IMPORTANT FACTORS TO CONSIDER WHEN CHOOSING ALGORITHM:
There are plenty of Algorithm that exists in the world of programming. Choosing the right Algorithm for your task might be bit tricky in nature. Programmers consider different factors while choosing an Algorithm but out of which there are two most important factor that every programmers will consider as top priority.
- Correctness : An algorithm should always fetch correct expected result under all circumstances. There are instances in mathematical operations where following different steps will yield different results. So a programmer should always analyze algorithm to check the correctness of the result.
- Efficiency: This is another important thing every programmer must consider. Efficiency of an Algorithm must be taken into account for efficient code. This Algorithm efficiency depends on different factors such as Speed of execution, Size of steps etc. Efficient Algorithm must be fast to execute and should occupy only less space when implemented in the code.
WRITE YOUR OWN ALGORITHM:
There are instances where none of the established Algorithms can help to perform the task you have at your hand. At these instances you have to write your own Algorithm and get your problem resolved. Designing your own Algorithm is a good practice as you can have more control over how a particular task is performed.
Hope this article helps you to get started with Algorithms. I will continue to write articles on different Algorithms and explain them along with the code. Happy programming 🙂
Quote and Order boards in minutes on: https://jlcpcb.com/quote
Latest posts by Frank Donald (see all)
- Bubble sort Algorithm Explained - December 10, 2018
- Cupboard lighting circuit using Transistor - November 1, 2018
- Rechargeable batteries and how to choose one for your projects - September 16, 2018