# Algorithm

 Date conversion 28.05.2018 Size 51.62 Kb.
Complexity Analysis

Algorithm: An Algorithm is a mechanism to manipulate data in data structure.

1. It is a step-by-step method of solving a problem or making decisions, as in making a diagnosis.

2. An established mechanical procedure for solving certain mathematical problems.

An algorithm is just the outline or idea behind a program. We express algorithms in pseudo-code: something resembling C or C++, but with some statements in English rather than within the programming language. It is expected that one could translate each pseudo-code statement to a small number of lines of actual code, easily and mechanically.

Properities of the Algorithm:
1) Finiteness: - an algorithm terminates after a finite numbers of steps.

2) Definiteness: - each step in algorithm is unambiguous. This means that the action specified by the step cannot be interpreted (explain the meaning of) in multiple ways & can be performed without any confusion.

3) Input:- an algorithm accepts zero or more inputs

4) Output:- it produces at least one output.

5) Effectiveness:- it consists of basic instructions that are realizable. This means that the instructions can be performed by using the given inputs in a finite amount of time

Computational Complexity

To compare the efficiency of algorithms, a measure of the degree of difficulty of an algorithm called Computational Complexity is developed.

Complexity analysis

Efficiency of an algorithm: the task of determine the computing time and storage space required of an algorithm is known as efficiency of an algorithm or performance analysis.

Two kind of efficiency:

1. Space efficiency.

2. Time efficiency.

Space Efficiency: The amount of temporary required for running the algorithm, it has two components.

1. Fixed static part: space for numbers, constants size of input and output Cp Fixed static part.

2. Variable dynamic part: variables whose size is dependent problem, Sp space required for variable dynamic part.

A total space requirement for an algorithm is the sum of both fixed and dynamic part.

S(P)= CP+SP

Example 1:

• Find sum of two integer numbers.

int x,y,sum;
sum = x+y;

In this code no variable dynamic part space for each integer variable is 4 byte.
We have 3 integer variable and space needed for x,y and sum = 12 bytes.

S(P)= CP+SP
S(P)= 12 + 0 = 12

Example 2:

• Find sum of array elements.

{

int total = 0, i;
for(i=0; itotal = total + x[i];
}
S(P)= CP+SP
S(P)= 4 X 4 + n = 16 + n

Time Efficiency: the amount of time to run the program is known as time efficiency or time complexity.

Total time taken by an algorithm is the sum of compile time and runtime.
 Example 1: a = a * b 1 unit of time. Example 2: for( i=0; ia = i + a; n unit of time.

Example 3:

for( i=0; ifor( j=0; ja = i + j;
1 unit of time.

Big O: Big O is a technique for comparing two or more algorithm.

Characteristics:

1. Comparison of algorithms without hardware consideration.

2. Simplification of equation of ease comparison.
 Example 1: Example 2:

Worst-case, Best-case and Average-case Efficiencies:

Let us the complexity function T(n) for certain cases.

1. Worst case: It gives the maximum value of T(n) for any possible input.

2. Best case: It gives the minimum value of T(n) for any possible input.

3. Average case: It gives the expected value of T(n)

Page of