Algorithm and Flow Chart
Algorithm and Flow Chart
Algorithms Basics
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.
From the data structure point of view, following are some important categories
of algorithms
Search − Algorithm to search an item in a data structure.
Sort − Algorithm to sort items in a certain order.
Insert − Algorithm to insert item in a data structure.
Update − Algorithm to update an existing item in a data structure.
Delete − Algorithm to delete an existing item from a data structure.
Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the
following characteristics – (Every Algorithm must satisfy the following
properties)
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.
Correctness- Every step of the algorithm must generate a correct output.
An algorithm is said to be efficient and fast, if it takes less time to execute and
consumes less memory space. The performance of an algorithm is measured on
the basis of following properties :
1. Time Complexity
2. Space Complexity
Space Complexity
Its the amount of memory space required by the algorithm, during the course of
its execution. Space complexity must be taken seriously for multi-user systems
and in situations where limited memory is available.
An algorithm generally requires space for following components :
Instruction Space: Its the space required to store the executable version of the
program. This space is fixed, but varies depending upon the number of lines of code in
the program.
Data Space: It’s the space required to store all the constants and variables (including
temporary variables) value.
Environment Space: It’s the space required to store the environment information
needed to resume the suspended function.
Time Complexity
Time Complexity is a way to represent the amount of time required by the
program to run till its completion. It's generally a good practice to try to keep the
time required minimum, so that our algorithm completes it's execution in the
minimum time possible.
NB: In design and analysis of algorithms, usually the second method is used to
describe an algorithm. It makes it easy for the analyst to analyze the algorithm
ignoring all unwanted definitions.
Writing step numbers, is optional.
We design an algorithm to get a solution of a given problem. A problem can be
solved in more than one ways.
The following table briefly tries to show the difference in all three notations –
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
Flowchart:
Example 3: Determine Whether a Temperature is Below or Above the
Freezing Point
Algorithm:
Step 1: START
Step 2: Input temperature,
Step 3: If it is less than 32, then print "below freezing point", otherwise
print "above freezing point"
Step 4: STOP
Flowchart:
Step1: Start
Step2: Store 20 in I
Step3: Display I
Step4: Add 2 to I
Step6: End
Example 8: Write an algorithm to find the smallest number between two
numbers
Step1: Start
Step 6: End