Algorithm, Notation, Performance Analysis
Algorithm, Notation, Performance Analysis
Algorithm, Notation, Performance Analysis
Introductory The algorithm name is followed by the brief Finds the largest algebraic element of
comment description of task the algorithm performs. vector A
The description gives names and types of the
variable used in the algorithm.
Steps Algorithm is made up of sequence of numbered Steps beginning with the phrase
steps. enclosed in the square brackets that
It describe action to be executed or tasks to be describes that sequence of an
performed algorithm.
Statements are executed in a left to right order.
if condition If the condition is TRUE the statement after then are executed
then
----------- If it is FALSE the statement after the else clause loop is
----------- executed
else
-----------
2.3. Case statements: It is used when a choice
among from several alternatives must be made.
2 Repeat while logical To repeat a step until a given logical expression is false.
expression The evaluation and the testing of the logical
expression is performed at the beginning of the loop
3 Repeat for INDEX = sequence Types 1 and 2 and is used to repeat a step for a
while logical expression. sequence whose values are taken successively by
INDEX until logical expression is false.
• Eg:
Repeat while true
read array A[I]
if there is no more data
then exit.
else
I=I+1
2.5. Go to and exit loop statements: Go to and
exit loop statements are conditional statements,
these causes unconditional transfer of control.
Go to statement: This statement Eg: Label :
causes unconditional control ----
transfer to the step referenced,
----
Regardless of the other statements.
Go to label.
• Example 1
int square(int a)
{
return a*a;
}
• n the above piece of code, it requires 2 bytes of memory
to store variable 'a' and another 2 bytes of memory is
used for return value.
• That means, totally it requires 4 bytes of memory to
complete its execution.
• And this 4 bytes of memory is fixed for any input value of
'a'. This space complexity is said to be Constant Space
Complexity.
• If any algorithm requires a fixed amount of space for all
input values then that space complexity is said to be
Constant Space Complexity.
• Consider the following piece of code...
• Example 2
int sum(int A[ ], int n)
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}
• In the above piece of code it requires
– 'n*2' bytes of memory to store array variable 'a[ ]'
– 2 bytes of memory for integer parameter 'n'
– 4 bytes of memory for local integer variables 'sum' and 'i' (2 bytes each)
– 2 bytes of memory for return value.