An algorithm can be defined as a finite sequence of instructions for performing a task or solving a problem. The instructions must be unambiguous and have a clear stopping point. Algorithms can be expressed in any language, from natural languages like English or French to programming languages like FORTRAN, C, Pascal, Java, PHP, etc.
We use algorithms every day. For example, a recipe for baking a cake is an algorithm. Most programs, with the exception of some artificial intelligence applications, consist of algorithms. Inventing elegant algorithms i.e. writing algorithms that are simple and require the fewest steps possible, is one of the principal challenges in programming.
The basic instructions used by the algorithm should be elementary so that these can be carried out by a machine, viz. the Computer. These instructions can be - add, subtract, read a character, write a character, compare numbers and find out which is larger, etc. Even though these basic instructions are few, it is possible to carry out a large variety of data processing tasks by a permutation and combination of these elementary tasks or instructions.
At this point it is necessary to understand the essential characteristics of Computers, which are as follows:
A computer can be thought of as a slave which carries out instructions at a very high speed obediently, uncritically and without exhibiting any emotions. It is the task of a programmer to give extensive, detailed and correct instructions for solving a problem.
In order to carry out a task using a computer, the following steps are followed:
While writing an algorithm, you must keep in mind the following basic features:
To develop a better understanding of what we mean by this, let us look at two examples. The first example is not an algorithm, the second one is.
This example is of a set of instructions that appear to be an algorithm, though it is not so.
Bengal gram flour 2 cups, sugar 2 cups, ghee 3 cups, milk ½ cup, Bournvita 6 tablespoons, Vanilla essence 5 drops, water ½ cup.
Step1: Warm 1 cup ghee. Put gram flour and fry for 2 minutes on low flame.
Step2: Separately dissolve Bournvita in warm milk and add vanilla essence.
Step3: Add 2 cups sugar in ½ cup water in a pan and warm it till sugar dissolves. Boil syrup till it become sticky.
Step4: Pour the prepared Bournvita in syrup and stir continuously.
Step5: Add fried gram flour to the syrup and continue stirring 15 to 20 minutes adding the remaining melted ghee until the mixture does not stick to the pan.
Step6: Pour the mixture on a plate smeared with melted ghee shaking while pouring to ensure uniform spreading.
Step7: After 10 to 15 minutes cut into 40 pieces.
40 pieces of Bournvita cake.
The above -
However, it requires someone to have a common cooking sense to execute these instructions. Instructions such as low flame and warm milk are not precise and require individual's judgment. How much quantity of vanilla essence is to be added is not specified. If you read carefully, you will notice several such instructions that are not precise & basic. Hence it does not qualify as an algorithm.
This example is of a set of instructions that is truly an algorithm.
Needles No. 12 = 2
Wool 4 ply = 9 balls
Step1: Cast on 133 stitches
Step2: Repeat steps 3 and 4, 11 times
Step3: Knit 2, Purl 1
Step4: Purl 1, Knit 1
Step5: Knit 1, Purl 2
Step15: Cast off 133 stitches
Result: A Sweater
The above instructions are -
By a permutation & combination of above elementary set of actions, an infinite number of patterns can be created. Due to the preciseness of instructions & a few basic tasks to be performed, it may be possible to design a machine to automate knitting.
A French engineer Jacquard designed a machine in 1801 on the above lines, which could perform the task of creating large number of patterns. This machine was similar to the modern day computers.
I believe the above would give you a fair understanding of what we mean by an Algorithm
CEO, Computer Solutions
Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of XLRI, industry professionals, and govt. officials.
Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.