Unit 1
Unit 1
Algorithms, building blocks of algorithms (statements, control flow, functions), notation (pseudo code, flow chart),
algorithmic problem solving for socio economic conditions in global perspectives, simple strategies for developing
algorithms (iteration, recursion), the efficiency of algorithms.
1.1 Algorithms
Properties OF Algorithm:
1. Finiteness: An algorithm must be executed finite number of times. The algorithm must stop,
eventually.Stopping may mean that you get the expected output OR you get a response that no
solution is possible. Algorithms must terminate after a finite number of steps.An algorithm should
not be infinite and always terminate after definite number of steps.
There is no point in developing an algorithm which is infinite as it will be useless for us.
2. Definiteness: Each step of the algorithm must be accurate and clear. Algorithms must specify
every step and the order the steps must be taken in the process.Definiteness means specifying the
sequence of operations for turning input into output. Algorithm should be clear and
unambiguous.Details of each step must be also be spelled out (including how to handle errors).It
should contain everything quantitative and not qualitative.
3. Effectiveness: Each step must be effective, in the sense that it should be primitive (easily
convertible into a program statement) and can be performed exactly in a finite amount of time.
4. Independent: The algorithm must be independent of any programming code.
5. Input/output: Each algorithm must take zero or more quantities as input data and give out
one or more output values.
List the data needed to solve the problem (input) and know what is the end result (output).
Describe the various step to process the input to get the desired output.
Break down the complex processes into simpler statements.
Finally test the algorithm with different data sets.
Efficiency: Algorithms streamline processes, leading to faster and more optimized solutions.
Reproducibility: They yield consistent results when provided with the same inputs.
Problem-solving: Algorithms offer systematic approaches to tackle complex problems
effectively.
Scalability: Many algorithms can handle larger datasets and scale with increasing input sizes.
Automation: They enable automation of tasks, reducing the need for manual intervention.
Disadvantages of Algorithms:
Algorithms can be constructed from basic building blocks namely, sequence, selection and
iteration.
Statements:
State:
Transition from one process to another process under specified condition with in a time is
called state.
Control flow:
The process of executing the individual statements in a given order is called control flow.
The control can be executed in three ways
1. sequence
2. selection
3. iteration
Sequence:
All the instructions are executed one after another is called sequence execution.
Example:
Selection:
A selection statement causes the program control to be transferred to a specific part of the
program based upon the condition.
If the conditional test is true, one part of the program will be executed, otherwise it will
execute the other part of the program.
Example
Write an algorithm to check whether he is eligible to vote?
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop
Iteration:
In some programs, certain set of statements are executed again and again based upon
conditional test. i.e. executed more than one time. This type of execution is called looping or
iteration.
Example
Functions:
Example:
Pseudocode:
Pseudocode is an informal high-level description of the operating principle of a computer program or algorithm.
It uses the basic structure of a normal programming language, but is intended for human reading rather than
machine reading.
It is text based detail design tool. Pseudo means false and code refers to instructions written in programming
language.
Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules. The
pseudocode is written in normal English language which cannot be understood by the computer.
Example:
Total=mark1+mark2+mark3
Average=Total/3
4. End multi-line structures Each structure must be ended properly, which provides more clarity.
Example:
READ a, b
IF a>b then
PRINT a is greater
ENDIF
Pesudocode must never written or use any syntax of any programming language
Advantages of Pseudocode
• Can be done easily on a word processor
• Easily modified
Disadvantages:
It is not visual
For a beginner, it is more difficult to follow the logic or write pseudo code as compared to flowchart.
Start: BEGIN
Stop: END
GET a, b
ADD c=a+b
PRINT c
END
Flowchart:
A graphical representation of an algorithm.
Flowcharts is a diagram made up of boxes, diamonds, and other shapes, connected by arrows.
Each shape represents a step in process and arrows show the order in which they occur.
Advantages of Flowchart
Communication:
Effective Analysis
With the help of flowchart, a problem can be analyzed in more effective way. Proper Documentation
Flowcharts are used for good program documentation, which is needed for various purposes.
Efficient Coding
The flowcharts act as a guide or blue print during the system analysis and program development phase.
The maintenance of operating program becomes easy with the help of flowchart
Disadvantages of Flowchart
Complex Logic: Sometimes, the program logic is quite complicated. In that case flowchart becomes complex and difficult
to use.
2. simplicity.
❖ An algorithm should be precisely defined and investigated with
mathematical expressions.
❖ Simpler algorithms are easier to understand and easier to program.
❖ Simple algorithms usually contain fewer bugs.
Coding an Algorithm
❖ Most algorithms are destined to be ultimately implemented as
computer programs. Programming an algorithm presents both a
peril and an opportunity.
❖ A working program provides an additional opportunity in allowing
an empirical analysis of the underlying algorithm. Such an analysis
is based on timing the program on several inputs and then
analysing the results obtained.
6.