Algorithms
Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output. Algorithms are generally created
independent of underlying languages, i.e. an algorithm can be implemented in more
than one programming language.
Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the
following characteristics −
Unambiguous − Algorithm should be clear and unambiguous. Each of its steps
(or phases), and their inputs/outputs should be clear and must lead to only one
meaning.
Input − An algorithm should have 0 or more well-defined inputs.
Output − An algorithm should have 1 or more well-defined outputs, and should
match the desired output.
Finiteness − Algorithms must terminate after a finite number of steps.
Feasibility − Should be feasible with the available resources.
Independent − An algorithm should have step-by-step directions, which should
be independent of any programming code.
Representation of Algorithm
An algorithm can be represented in two ways:
i) Pseudocode and ii) Flowchart
Pseudocode:
Pseudocode is a form of Structured English for writing an algorithm.
It uses programming-style constructs, but is not written in an actual
programming language.
You do not need to worry about the detailed syntax or be precise about how the
code will complete a particular task.
Writing in pseudocode helps you concentrate on the logic
(Process) and efficiency of your algorithm before you have to start thinking
about the actual code you will be using.
It can’t be compiled or interpreted by the computer.
There are three basic constructs that are used to write algorithms in pseudocode:
Sequence - This is writing the steps down in the order that they need to happen.
Selection - This is the IF … THEN … ELSE constructs that allow you to
choose between options.
Iteration - Finally there is iteration (loop) constructs that you will learn when
you program - These are FOR … UNTIL … WHILE.
Example 1. Algorithm/pseudocode to find the sum of two numbers
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Example 2. Algorithm/pseudocode to find the largest of three input numbers
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
Example 3. Algorithm/pseudocode to find the roots of quadratic equation ax2+bx+c=0
Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2;
Step 3: Calculate discriminant
D←b*b–4*a*c
Step 4: If D ≥ 0
r1 ← (-b + sqrt(D))/(2*a)
r2 ← (-b – sqrt(D))/(2*a)
Display r1 and r2 as roots.
Else
Display roots are imaginary
Step 5: Stop
Flowchart:
Flowchart is a diagrammatic representation of an algorithm. Complex programs
can be drawn in a simple way using a flowchart. Communication with other people
becomes easy by drawing flowcharts and sharing them.
Flowchart Symbols
Here is a chart for some of the common symbols used in drawing flowcharts.
Symbol Symbol Name Purpose
Used at the beginning and end of the algorithm to show start and
Start/Stop
end of the program.
Process Indicates processes like mathematical operations.
Input/ Output Used for denoting program inputs and outputs.
Stands for decision statements in a program, where answer is
Decision
usually Yes or No.
Arrow Shows relationships between different shapes.
On-page Connects two or more parts of a flowchart, which are on the same
Connector page.
Off-page Connects two parts of a flowchart which are spread over different
Connector pages.
Guidelines for Developing Flowcharts
These are some points to keep in mind while developing a flowchart −
Flowchart can have only one start and one stop symbol
Off-page connectors are referenced using alphabets
General flow of processes is top to bottom or left to right
Arrows should not cross each other
Advantages of Flowchart
It is the most efficient way of communicating the logic of system.
It act like a guide for blueprint during program designed.
It also helps in debugging process.
Using flowchart we can easily analyze the programs.
flowcharts are good for documentation.
Disadvantages of Flowchart
Flowcharts are difficult to draw for large and complex programs.
It does not contain the proper amount of details.
Flowcharts are very difficult to reproduce.
Flowcharts are very difficult to modify.
Example 1. Flowchart to check whether a number is even or odd.
Example 2. Flowchart to find the maximum of three numbers.
Example 2. Flowchart to calculate simple interest given the values of P, R and N.