¤ Home » Programming » 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:

- The given task is analyzed.
- Based on the analysis an algorithm to carry out the task is formulated. The algorithm has to be precise, concise and unambiguous.
- 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)
- 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.

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

- 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.
- An algorithm must result in one or more output(s). The outputs desired must therefore be identified.
- 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.
- 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.
- 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.
- 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.

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

**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.

The above -

- is a sequence of instructions or procedure to solve the problem of making a Bournvita cake (steps 1 - 7).
- requires a set of inputs - the ingredients.
- produces an output - 40 pcs of cake.
- 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.

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

**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

The above instructions are -

- Precise (unambiguous)
- 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

comments powered by Disqus

How to move your Email accounts from one hosting provider to another without losing any mails?

Self Referential Data Structure in C - create a singly linked list

How to resolve the issue of receiving same email message multiple times when using Outlook?

Moving Email accounts from one cPanel server to another

Elements of the C Language - Identifiers, Keywords, Data types and Data objects

Mosquito Demystified - interesting facts about mosquitoes

What is a Firewall and why is it important for network security?

**Rajeev Kumar**

CEO, Computer Solutions

Jamshedpur, India

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.

Copyright © How2Lab.com. All rights reserved.