Tuesday, March 1, 2016

What’s an Algorithm ?

Dear Folks, 
                        An algorithm is backbone of a program. Every program is an algorithm, but an algorithm is not a program. When we add syntactical aspects of a particular programming language to it, we call it as  a program. 

This is actually a name of a person called , Alkhowrizmi, an  Arab   mathematician   of the court of Mamun in Baghdad. His treatises on Hindu arithmetic and on algebra made him famous.He is said to  have given algebra  its  name, and the word algorithm issaid to have been derivedfrom his name. Much of the mathematical knowledge of medieval Europe was derived from Latintranslations of his works.

         Basically an algorithm is a precise set of instructions, to perform a particular task step by step. 
Let’s develop an algorithm to prepare a cup of coffee ...

Step 1: Start
Step 2: Take a cup-full hot milk
Step 3: Add a half tea spoon full coffee powder
Step 4: Add a tea-spoon full sugar
Step 5: Stir well, so that the coffee powder can mix properly with milk.
step 6: Stop
I guess a normal person agrees that, it is an algorithm, Believe me………                                           IT IS NOT AN ALGORITHM !!!
Because, It is  not precise !  as It does not specify quantity of milk,powder and sugar. Moreover how long I should stir, the time is also not specified. If dictated  to a normal person, he could prepare a cup of coffee, by using his common sense… but computer has no common sense kind of thing, so we must specify everything precisely, as a programmer, you  must always keep this point in mind that a computer is a garbage in garbage out....
Nothing should be vague. As the above set of steps are all vague, It can not be called an algorithm for preparing a cup of coffee. Let me demonstrate , how we develop the an algorithm for  of swapping two variables without using the third one.The image explains you, how to exchange contents of the two glasses.
From the image it clear that, to swap/exchange contents , we must use a temporary container. But in programming it is possible to swap/exchange without using a  temporary container. Let’s develop an algorithm to swap two numbers say A and B using temporary container....
Step 1: Start
Step 2: Let A:=5 and B:=7 and C be the temporary container
step 3: (Step 1 of image)  C := A
step 4:(Step 2 of image)  A := B
Step 5:(Step 3 of image)  B := C
Step 6: Print A and B
This will print A as 7 and B as 5. This is how we exchange/swap two variables using third variable. Here A is the first variable and B is the second variable and C is the third variable(temporary variable). This algorithm can be converted to a program, just by adding syntax of a given language.
// A Java program to swap two numbers using third variable
class DemoSwapping
{
public static void main(String[] args)
{
int A,B,C;
A=5;
B=7;
/* swap here */
  C = A;
  A = B;
  B = C;
System.out.print(“A=”+A);
System.out.println(“  B=”+B);
}// End of main method
}//End of class DemoSwapping : Output : A=7 B=5
Please give suggestions and comments through this blog itself... Thanks for reading... 

No comments:

Post a Comment