ALGORITHM


Def: - An Algorithm is a step by step process to solve a given problem.

Algorithm is also known as Pseudo code.
Algorithm is an effective method for solving a problem expressed as a finite sequence of steps.
Each step in the Algorithm is known as an instruction.
Each algorithm is a list of well-defined instructions for completing a task.
An algorithm consists of a set of explicit and unambiguous finite steps which, when carried out for a given set of initial conditions, produce the corresponding output and terminate in a fixed amount of time.
By unambiguity it is meant that each step should be defined precisely i.e., it should have only one meaning

According to D.E.Knuth, a pioneer in the computer science discipline, an algorithm has five important features.

i)                Finiteness       An algorithm terminates after a fixed number of steps.
ii)              Definiteness   Each step of the algorithm is precisely defined, i.e., the actions to be carried out should be specified unambiguously.
iii)            Effectiveness All the operations used in the algorithm are basic (division, multiplication, comparison, etc.) and can be performed exactly in a fixed duration of time.
iv)             Input               An algorithm has certain precise inputs, i.e. quantities, which are specified to it initially, before the execution of the algorithm begins.
v)               Output            An algorithm has one or more outputs, that is, the results of operations which have a specified relation to the inputs.

Let us take some example to illustrate all the features of an algorithm.

Example 1:     Algorithm to find sum of two numbers.
Step 1: Start
Step 2: Define a, b
Step 2: Read a, b
Step 3: sum=a+b
Step 4: Print the sum
Step 5: Stop.

Example 2:     Algorithm to find whether a number is even or odd.
Step 1: Start
Step 2: Define a
Step 3: Read a
Step 4: If a%2==0 goto step 5 else goto step 6.
Step 5: print EVEN; goto step 7
Step 6: print ODD
Step 7: Stop.
Example 3:     Algorithm to find largest of three numbers.
Step 1  : Start
Step 2  : Define A, B and C
Step 3  : Read three numbers A, B and C
Step 4  : If A>B goto step 5 else goto step 8
Step 5  : If A>C goto step 6 else goto step 7
Step 6  : Print A is Large , goto step 10
Step 7  : Print C is Large , goto step 10
Step 8  : If B>C goto step 9 else goto step 7
Step 9  : Print B is Large
Step 10: Stop.

Example 4:     Algorithm to find Fibonacci Sequence
Step 1  : Start
Step 2  : Define a=0,b=1,c,i,n
Step 3  : Read n
                        Step 4  : print a, b
Step 5  : i =3
                        Step 6  : If i < = n goto step 7 else goto step 12
                        Step 7  : c=a+b
                        Step 8  : print c
                        Step 9  : a=b, b=c
                        Step 10: i=i+1
                        Step 11: If i < = n goto step 7 else goto step 12
                        Step 12: Stop

Advantages of Algorithms

1. Provides detailed and most efficient solution to the given problem.
2. Facilitates program development by acting as design document for a given problem.
3. Ensures easy understanding of a problem solution.
4. Simplifies the identification and removal of logical errors in the program.
5. Programmer or user understands the problem in easy and detailed manner as we are analyzing 
    problem in step-by-step process.

Disadvantages of Algorithms

1. In large algorithms, the flow of program control becomes difficult to understand.
2. Lacks visual representation of programming structure.
3. Too many steps in the algorithm may confuse the programmer.

No comments:

Post a Comment