In this tutorial, we are discussing writing algorithms. There are no well-defined standards or rules for writing algorithms. Rather, it is problem and resource-dependent. Algorithms are never written to support a particular programming code.
Writing Algorithms of the program before starting coding is a very beneficial habit in computer programming. It resolves various errors, and confusion while programming for big projects.
Algorithms provide a better understanding of programming and help in building better problem-solving logic. Algorithms help the programmer to write code faster by following steps in the Algorithm.
As we know that all programming languages share basic code constructs like loops (do, for, while), flow-control (if-else), etc. These common constructs can be used to write an algorithm.
Let’s try to learn algorithm-writing by using an example.
Problem – Write an algorithm to multiply two numbers and display the result.
step 1 – START step 2 – declare three integers a, b & c step 3 – define values of a & b step 4 – multiply values of a & b step 5 – store output of step 4 to c step 6 – print c step 7 – STOP
We design an algorithm to get a solution to a given problem. There can be more than one solution to solve the given problem.
Find the Fibonacci series till the term is less than 1000
Step 1: Start Step 2: Declare variables first_term,second_term and temp. Step 3: Initialize variables first_term ← 0 second_term ← 1 Step 4: Display first_term and second_term Step 5: Repeat the steps until second_term ≤ 1000 5.1: temp ← second_term 5.2: second_term ← second_term + first_term 5.3: first_term ← temp 5.4: Display second_term Step 6: Stop
How to write an effective Algorithm?
For writing algorithms, the following steps are the very important.
Step 1: Obtain the detailed information on the problem.
It is a very important step in writing algorithms. Before starting with an algorithm the programmer must obtain maximum information about the problem that needs to be solved.
This step will help the programmer to get a better understanding of the problem which will surely prove to be useful while solving the problem.
Step 2: Analyze the problem.
Proper Analysis of the problem must be done including the data that must be gained, processed, and retrieved for generating a valid output.
This step helps the programmer with various processes that must be attained while generating the output along with structure and type of data.
Step 3: Think of a problem-solving approach.
This is a very important and the most difficult step in writing algorithms. The programmer has to come up with a problem-solving approach that will help us to build the model to solve the given problem.
Experience and practice is an important factor in thinking about the problem-solving approach. So make sure you try writing different algorithms and reading algorithms that will assist you in better understanding.
Step 4: Review the problem-solving approach and try to think of a better Alternative.
A high-quality Algorithm must contain the best approach to solve a problem which will help in reducing the effort in coding as well as decrease the time complexity and size of the program.
Therefore, when you think of a problem-solving approach try thinking of a better alternative with better results, this will help you generate a finer programming logic.
Also, review the problem-solving approach, and make sure the problem will be solved through it.
Step 5: Develop a basic structure of the Algorithm.
Develop a basic structure of the problem-solving approach; explain the approach step-by-step with a short and effective description.
Step 6: Optimize, Improve and refine.
After developing an Algorithm try optimizing the explanation to increase its readability and accessibility of it.
Also, try Improving and refining the algorithm for better understanding and efficient use.