Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Algorithm Specifications

Pseudocode Conventions
Psuedo code Purpose
• Flowcharts graphically represent the
algorithm.
• But it can only be used for simple
algorithms or problems.
• So psuedo code is used to represent
algorithm
Psuedocode Conventions
Conventions - Comments
• Begin with //
• Continue until the end of line
Conventions - Blocks
• Indicated with matching braces:{ and }.
• Statement delimiter ;
• Examples
– A compound statement
– The body of a procedure
Conventions - Identifier
• Begins with a letter.
• Implicit data type assignment
• Scope depends on context
Conventions- Data Types
• Simple
– Integer, float, char, Boolean, and so on.
• Compound
– Formed with records

node = record
{ Datatype_1 data_1;
---------------
---------------
Datatype_n data_n;
Node *link; }
Conventions - Assignment
• Assignment of values to variables
• Assignment statement format

<variable> := <expression>
Conventions - Boolean
• Two boolean values
– True , False.
• Produced using operators
– Logical
• and, or , not
– Relational operators
• <, <= , = , # , >= , >
Conventions - Array
• Array indices start at zero.
• Elements represented using [ ]
• Example
– (i,j) th element in A denoted as A[i,j].
Conventions - Loops
• Looping statements
– For
– While
– Repeat-until
Conventions – Loop Contd..
• While Loop
while <condition> do while (variable <=n) do
{ {
<statement 1> <statement 1>
--------------- ………….
<statement n> …………
} <statement n>
variable :=variable + incr;
}
Conventions - Loop Contd..
• For Loop

for variable:= value1 to value2 step stepval


do
{
<statement 1>
:
:
<statement n>
}
Conventions - Loop Contd..
Repeat-until loop

Repeat
<statement 1>
--------- --
<statement n>
Until <condition>
Conventions - Condition
Conditional statement in two forms:
• Form1
If <condition> then <statement>
• Form2

If <condition> then <statement 1> else <statement 2>


Conventions - Input / Output
• Input
– read
• Output
– write
• No format is used to specify the size of input
or output quantities.
Conventions - Procedure
• Only one type procedure - Algorithm.
• Consists of a heading and a body.
• Heading format

Algorithm name (<parameter list>)


Example Algorithm
• An algorithm to find the maximum in array
of n numbers
Algorithm max(A , n)
// a is an array of size n
{
Result:= A[1];
for i = 2 to n do
if (A[i] > Result) then Result := A[i];
return Result;
}
Selection Sort Algorithm
Difference between Algorithm and Pseudo code
Algorithm for Insertion Sort
Pseudocode for Insertion Sort
What is Performance Analysis of an algorithm?
Performance Analysis
• Does it do, what we want it to do?
• Does it work correctly according to the
original specifications of the task?
• Is there documentation that describes how to
use it and how it works?
• Are procedures created in such a way that
they perform logical sub functions?
• Is the code readable?
Performance Analysis
• Space Complexity : It is the amount of
memory it needs to run to completion.
• Time Complexity: The time complexity of an
algorithm is the amount of computer time it
needs to run to .completion
Performance Evaluation
• a priori estimates
• a posteriori testing
Space Complexity
• When we design an algorithm to solve a
problem, it needs some computer memory to
complete its execution.
Components of space complexity

• There are three components of space complexity:


• Instruction or Code Space:
• This space holds the collected version of the program
instructions.
• Data space:
• This space holds all the static data such as constant,
variables and dynamic data such as growing structure.
• Environment Stack Space:
• This space is used to store return address for the functions.
So that,execution can begin again where they left earlier
and it may also be used for parameters.

You might also like