¤ Home » Programming » What is an Algorithm?

# What is an Algorithm?

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:

• Computers are built to carry out a small variety of elementary instructions. It is not necessary to have more than fifty types of instructions even for a very versatile machine.
• Each instruction is carried out in less than a millionth of a second.
• Each instruction is carried out obediently with no questions asked.
• Each instruction is carried out without making a mistake.

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:

1. The given task is analyzed.
2. Based on the analysis an algorithm to carry out the task is formulated. The algorithm has to be precise, concise and unambiguous.
3. The algorithm is eventually used as a reference to write the final computer program. (for more on this, read the beginners guide to writing Programs)
4. The computer program is fed to the computer for execution. The computer interprets the program, carries out the instructions given in the program and computes the results.

## Features of an Algorithm

While writing an algorithm, you must keep in mind the following basic features:

1. A number of quantities are provided to an algorithm initially before the algorithm begins. These quantities are the inputs which are processed by the algorithm.
2. An algorithm must result in one or more output(s). The outputs desired must therefore be identified.
3. The processing rules specified in the algorithm must be precise, unambiguous and lead to a specific action. In other words the instructions must not be vague. It should be possible to carry out the given instruction.
4. Each instruction must be sufficiently basic such that it can, in principle, be carried out in a finite time by a person with pen and paper.
5. The total time to carry out all steps in the algorithm must be finite. An algorithm may contain repetitive type of instructions. This requirement implies that the number of repetitions must be finite.
6. An algorithm must have one or more output.

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.

## Example 1

This example is of a set of instructions that appear to be an algorithm, though it is not so.

### Recipe for Bournvita Cake

Ingredients/Inputs:
Bengal gram flour 2 cups, sugar 2 cups, ghee 3 cups, milk ½ cup, Bournvita 6 tablespoons, Vanilla essence 5 drops, water ½ cup.

Method/Procedure:

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.

Result:
40 pieces of Bournvita cake.

##### Observations:

The above -

1. is a sequence of instructions or procedure to solve the problem of making a Bournvita cake (steps 1 - 7).
2. requires a set of inputs - the ingredients.
3. produces an output - 40 pcs of cake.
4. takes a finite time to execute the instructions & stop.

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.

## Example 2

This example is of a set of instructions that is truly an algorithm.

### Instructions to knit a sweater

Inputs:
Needles No. 12 = 2
Wool 4 ply = 9 balls

Method/Procedure:

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

:

:

(Similar steps)

Step15: Cast off 133 stitches

Result: A Sweater

##### Observations:

The above instructions are -

1. Precise (unambiguous)
2. Very few basic actions like Knit, Purl, Count, Cast on or off needles

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

# Share:

## Web Service Offers

Looking to build a website?
Launch a 5 page website in 1 day at only Rs.499/year. Hosting included.