0% found this document useful (0 votes)
20 views

Algorithm

Docu

Uploaded by

DURAIMURUGAN M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Algorithm

Docu

Uploaded by

DURAIMURUGAN M
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

PROBLEM SOLVING AND PYTHON

PROGRAMMING

INTRODUCTION TO ALGORITHMS

Unit 1

Dr. Srideivanai Nagarajan


Associate Professor
Algorithms
Definition of Algorithm
 The word Algorithm means ” A set of finite rules or
instructions to be followed in calculations or other
problem-solving operations ”
Or
” A procedure for solving a mathematical problem in a
finite number of steps that frequently involves
recursive operations”.

Therefore Algorithm refers to a sequence of finite steps to


solve a particular problem.
Algorithms
Use of the Algorithms:
Algorithms play a crucial role in various fields and have
many applications. Some of the key areas where
algorithms are used include:

1. Computer Science: Algorithms form the basis of


computer programming and are used to solve problems
ranging from simple sorting and searching to complex
tasks such as artificial intelligence and machine learning.
2. Mathematics: Algorithms are used to solve
mathematical problems, such as finding the optimal
solution to a system of linear equations or finding the
shortest path in a graph.
3. Operations Research: Algorithms are used to
optimize and make decisions in fields such as
transportation, logistics, and resource allocation.
Use of the Algorithms:

4. Artificial Intelligence: Algorithms are the foundation of


artificial intelligence and machine learning, and are used to
develop intelligent systems that can perform tasks such as
image recognition, natural language processing, and
decision-making.
5. Data Science: Algorithms are used to analyze, process, and
extract insights from large amounts of data in fields such as
marketing, finance, and healthcare.
These are just a few examples of the many applications of
algorithms. The use of algorithms is continually expanding as
new technologies and fields emerge, making it a vital
component of modern society.
Characteristics of an
Algorithm
Characteristics of an Algorithm
1. Clear and Unambiguous: The algorithm should be
unambiguous. Each of its steps should be clear in all
aspects and must lead to only one meaning.

2. Well-Defined Inputs: If an algorithm says to take inputs, it


should be well-defined inputs. It may or may not take input.

3. Well-Defined Outputs: The algorithm must clearly define


what output will be yielded and it should be well-defined as
well. It should produce at least 1 output.

4. Finite-ness: The algorithm must be finite, i.e. it should


terminate after a finite time.
Characteristics of an Algorithm
5. Feasible: The algorithm must be simple, generic, and
practical, such that it can be executed with the available
resources. It must not contain some future technology or
anything.

6. Language Independent: The Algorithm designed must be


language-independent, i.e. it must be just plain instructions that
can be implemented in any language, and yet the output will be
the same, as expected.

7. Input: An algorithm has zero or more inputs. Each that


contains a fundamental operator must accept zero or more
inputs.

8. Output: An algorithm produces at least one output. Every


instruction that contains a fundamental operator must accept
zero or more inputs.
Characteristics of an Algorithm
9. Definiteness: All instructions in an algorithm must be
unambiguous, precise, and easy to interpret. By referring to any
of the instructions in an algorithm one can clearly understand
what is to be done. Every fundamental operator in instruction
must be defined without any ambiguity.

10. Finiteness: An algorithm must terminate after a finite


number of steps in all test cases. Every instruction which
contains a fundamental operator must be terminated within a
finite amount of time. Infinite loops or recursive functions
without base conditions do not possess finiteness.

11. Effectiveness: An algorithm must be developed by using very


basic, simple, and feasible operations so that one can trace it out
by using just paper and pencil
BUILDING BLOCKS OF ALGORITHMS (statements,
state, control flow, functions)
Algorithms can be constructed from basic building blocks
namely, sequence, selection and iteration.
1. Statements:
Statement is a single action in a computer.
In a computer statements might include some of the following
actions
Ø input data-information given to the program
Ø process data-perform operation on a given input
Ø output data-processed result
2. State:
Transition from one process to another process under specified
condition with in a time is called state.
BUILDING BLOCKS OF ALGORITHMS (statements,
state, control flow, functions)
3. 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
4. Sequence:
All the instructions are executed one after another is called
sequence execution.
Example:

Add two numbers:


Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b
Step 4: Display c
Step 5: Stop
Selection
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 Selection

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
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
Algorithm to print all natural numbers up to n
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop
Functions:
Functions:
Function is a sub program which consists of block of code(set of
instructions) that performs a particular task.
For complex problems, the problem is been divided into smaller
and simpler tasks during algorithm design.
Benefits of Using Functions
Reduction in line of code
code reuse
Better readability
Information hiding
Easy to debug and test
Improved maintainability
Example for functions
Example:
Algorithm for addition of two numbers using
function
Main function()
Step 1: Start
Step 2: Call the function add()
Step 3: Stop
sub function add()
Step 1: Function start
Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
Step 5: Return
Example
Write an algorithm to find area of a rectangle

Step 1: Start
Step 2: get l,b values
Step 3: Calculate A=l*b
Step 4: Display A
Step 5: Stop
Exercises
Write algorithm for the following:
1. Calculating simple interest
2. Greatest of two numbers
3. Even or odd
4. Leap year or not
5. Positive or negative
6. Greatest of three numbers
7. Print all natural numbers upto n
8. Positive or negative or zero
9. Print n odd numbers
10. Print square of a number
11. Print cube of a number
Simple interest

Step 1: Start
Step 2: get P, n, r value
Step3:Calculate
SI=(p*n*r)/100
Step 4: Display S
Step 5: Stop
Greatest of two numbers
Step 1: Start
Step 2: get a,b value
Step 3: check if(a>b) print a is greater
Step 4: else b is greater
Step 5: Stop
Leap year or not

To check leap year or not


Step 1: Start
Step 2: get y
Step 3: if(y%4==0) print leap year
Step 4: else print not leap year
Step 5: Stop
positive or negative
To check positive or negative
number
Step 1: Start
Step 2: get num
Step 3: check if(num>0) print a is
positive
Step 4: else num is negative
Step 5: Stop
odd or even number

To check odd or even number


Step 1: Start
Step 2: get num
Step 3: check if(num%2==0) print
num is even
Step 4: else num is odd
Step 5: Stop
greatest of three numbers
To check greatest of three
numbers
Step1: Start
Step2: Get A, B, C
Step3: if(A>B) goto Step4 else goto
step5
Step4: If(A>C) print A else print C
Step5: If(B>C) print B else print C
Step6: Stop
check whether given number is
+ve, -ve or zero.
Algorithm to check whether given
number is +ve, -ve or zero.
Step 1: Start
Step 2: Get n value.
Step 3: if (n ==0) print “Given
number is Zero” Else goto step4
Step 4: if (n > 0) then Print “Given
number is +ve”
Step 5: else Print “Given number
is -ve”
Step 6: Stop
print all natural
numbers up to n
Write an algorithm to print all natural numbers up to n
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 8
Step 5: Print i value
step 6 : increment i value by 1
Step 7: go to step 4
Step 8: Stop
print n odd numbers
Write an algorithm to print n odd
numbers
Step 1: start
step 2: get n value
step 3: set initial value i=1
step 4: check if(i<=n) goto step 5
else goto step 8
step 5: print i value
step 6: increment i value by 2
step 7: goto step 4
step 8: stop
print n even numbers
Write an algorithm to print n even
numbers
Step 1: start
step 2: get n value
step 3: set initial value i=2
step 4: check if(i<=n) goto step 5
else goto step8
step 5: print i value
step 6: increment i value by 2
step 7: goto step 4
step 8: stop
squares of number from 1 to n
Write an algorithm to print squares of a
number
Step 1: start
step 2: get n value
step 3: set initial value i=1
step 4: check i value if(i<=n) goto step 5 else
goto step8
step 5: print i*i value
step 6: increment i value by 1
step 7: goto step 4
step 8: stop
Cubes of number from 1 to n
Write an algorithm to print to
print cubes of a number
Step 1: start
step 2: get n value
step 3: set initial value i=1
step 4: check i value if(i<=n) goto
step 5 else goto step8
step 5: print i*i *i value
step 6: increment i value by 1
step 7: goto step 4
step 8: stop

You might also like