Introduction to
Algorithm
Prepared By
Mrs. D J Bonde
Assistant Professor, MMIT
What is Algorithm
An algorithm is a collection of well defined ,unambiguous and effectively uncountable
instructions, if execute it will give proper output.
• Well-defined :- The instruction given in the algorithms should be simple & well
defined.
• Unambiguous :-The instruction should be clear, there should not be ambiguity.
• Effectively countable :- The instruction should be written step by step, which help
computer to understand the flow of control.
• We often use algorithm in our day-to-day life,but when and how?
Our cooking receipe
Our daily routine as a student
When we buy something
When we go outing
Our class routine
How it helps?
• To understand the usage of algorithm,look over the following conversation.
Lets discuss about the conversation, tom wants brush his teeth,so he asks chitti to bring
brush, what happens chitti returns cleaning brush.
Why this was happened ?
Because the statement given by tom was not well defined and it is ambiguous statement
so chitti get confused and bring some brush to Tom. This is what happen if the user gives
ambiguity statement to the computer. Therefore an algorithm should be simple and well
defined.
How an algorithm should be?
It should be in simple English ,what a programmer wants to say. It has a start,a middle
and an end. Probably an algorithm should have,
Probably an algorithm should have,
Start
[Link] the middle it should have set of tasks that computer wants to do and it should
be in simple English and clear.
[Link] avoid ambiguous should give no for each step.
Stop
Lets look over the simple example,
The following algorithm helps the computer to validate user’s email address.
Start
Create a variable to get the user’s email address
clear the variable , incase its not empty.
Ask the user for an email address.
Store the response in the variable.
Check the stored response to see if it is a valid email address
Not valid? Go back
Stop
Lets see how it works?
Why this Happened?
This was happened because the instructions given
in an algorithm does not have numbering for each
step. So Chitti gets confused which step have to
do.
To avoid this ambiguity ,we should number each
step while writing an algorithm. So lets rewrite the
algorithm ....
Step1: Start
Step2: Create a variable to get the user’s email
address Step3: Clear the variable,incase its not
empty.
Step4: Ask the user for an email address.
Step5: Store the response in the variable.
Step6: Check the stored response to see if it is a
valid email address
Step7: Not valid?Go back
Step8: Stop
Dr. Paul refeed Algorithm
Algorithm Template Format
• The real power of using a template to describe each algorithm is that you
can quickly compare and contrast different algorithms and identify
commonalities in seemingly different algorithms. Each algorithm is
presented using a fixed set of sections that conform to this template.
• We may omit a section if it adds no value to the algorithm description or
add sections as needed to illuminate a particular point.
Name
• A descriptive name for the algorithm. We use this name to communicate
concisely the algorithm to others. For example, if we talk about using a
Sequential Search , it conveys exactly what type of search algorithm we
are talking about. The name of each algorithm is always shown in Bold
Font .
Input/Output
Describes the expected format of input data to the algorithm and the
resulting values computed.
Cont..
Context
• A description of a problem that illustrates when an algorithm is useful and when
it will perform at its best. A description of the properties of the problem/solution
that must be addressed and maintained for a successful implementation. They are
the things that would cause you to choose this algorithm specifically.
Solution
• The algorithm description using real working code with documentation. All code
solutions can be found in the associated code repository.
Analysis
A synopsis of the analysis of the algorithm, including performance data and
information to help you understand the behavior of the algorithm. Although the
analysis section is not meant to “prove” the described performance of an algorithm,
you should be able to under‐ stand why the algorithm behaves as it does. We will
provide references to actual texts that present the appropriate lemmas and proofs to
explain why the algorithms behave as described
Building Blocks of Algorithms
• It has been proven that any algorithm can be constructed
from just three basic building blocks.
• These three building blocks are Sequence, Selection, and
Iteration(Repetition).
• Sequence
• This describes a sequence of actions that a program carries out one after
another, unconditionally. Execute a list of statements in order.
Algorithm for Addition of two numbers:
Step1: Start
Step 2: Get two numbers as input and store it in to a and b
Step 3: Add the number a & b and store it into c
Step 4: Print c
Step 5: Stop.
Cont..
• Selection
• Selection is the program construct that allows a program to choose between
different actions. Choose at most one action from several alternative
conditions.
Algorithm for path choser
Step 1: Check for the destination located from current position.
Step 2:If it is located in right then choose right way
Step 3:If it is located in left then choose left way.
Step 4:Else comeback and search for new way.
Path has been chosen successfully.
Cont..
• Algorithm to find biggest among 2 nos:
Step1: Start
Step 2: Get two numbers as input and store it in to a and b
Step 3: If a is greater than b then
Step 4: Print a is big
Step 5: else
Step 6: Print b is big
Step 7: Stop
Cont..
Repetition
• Repetition(loop) may be defined as a smaller program
the can be executed several times in a main program.
Repeat a block of statements while a condition is true.
Algorithm for Washing Dishes
Step1: Stack dishes by sink.
Step 2: Fill sink with hot soapy water.
Step 3: While more Dishes
Step 4: Get dish from counter,Wash dish
Step 5: Put dish in drain rack.
Step 6: End While
Step 7: Wipe off counter.
Step 8: Rinse out sink.
Algorithm to calculate factorial no
Step1: Start
Step 2: Read the number num.
Step 3: Initialize i is equal to 1 and fact is equal to 1 Step 4:
Repeat step4 through 6 until I is equal to num
Step 5: fact = fact * i
Step 6: I = i+1
Step 7: Print fact
Step 8: Stop
Algorithm Notations(Expressing Algorithms)
• As we know that ,an algorithm is a sequence of finite instructions, often
used for calculation and data processing.
Pseudocode
• “Pseudo” means imitation or false and “code” refers to the
instructions written in a programming language.
• Pseudocode is another programming analysis tool that is used for
planning a program
• Pseudocode is also called Program Design Language(PDL)
Guidelines to write Pseudocode
• Pseudocode is written using structured English.
• Pseudocode should be concise
• Keyword should be in capital letter
Pseudocode is made up of the following logic structure ,
• Sequential logic
• Selection logic
• Iteration logic
• Sequence Logic
• It is used to perform instructions in a sequence,that is one after another
• Thus,for sequence logic ,pseudocode instructions are written in an order in which they are to be
performed.
• The logic flow of pseudocode is from top to bottom
Pseudocode to add two numbers:
START
READ a,b
COMPUTE c by adding a &b
PRINT c
STOP
Selection Logic
It is used for making decisions and for selecting the proper path
out of two or more alternative paths in program logic.
It is also known as decision logic.
Selection logic is depicted as either an IF..THEN or an
IF…THEN..ELSE Structure.
Pseudocode to find bigger numbers:
START
READ a and b
IF a>b
THEN PRINT “A is big”
ELSE PRINT “B is big”
ENDIF
STOP
Repetition Logic
• It is used to produce loops when one or more instructions may
be executed several times depending on some conditions .
• It uses structures called DO_WHILE,FOR and
REPEAT__UNTIL
• Pseudocode to print first 10 natural numbers
START
INITIALIZE a=0
WHILE a<10
PRINT a
ENDWHILE
STOP
Flowchart
• A flowchart is a visual representation of the sequence of
steps and decision needed to perform a process.
• Flowchart for computer programming/algorithms
• As a visual representation of data flow, flowcharts are useful in
writing a program or algorithm and explaining it to others or
collaborating with them on it.
• You can use a flowchart to spell out the logic behind a program
before ever starting to code the automated process. It can help to
organize big-picture thinking and provide a guide when it comes
time to code. More specifically, flowcharts can:
o Demonstrate the way code is organized.
o Visualize the execution of code within a program.
o Show the structure of a website or application.
o Understand how users navigate a website or program.
Flow Chart Symbol
• Here are some common flowchart symbols
Cont..
Flowchart is made up of the following logic structure ,
Sequential logic
Selection logic
Iteration logic
• Sequence Logic
• In a computer program or an algorithm, sequence involves
simple steps which are to be executed one after the other.
The steps are executed in the same order in which they are
written. Below is an example set of instructions to add two
numbers and display the answer.
Cont..
Selection Logic
• Selection is used in a computer program or
algorithm to determine which particular step
or set of steps is to be executed. This is also
referred to as a ‘decision’.
• A selection statement can be used to choose a
specific path dependent on a condition.
• There are two types of selection:
binary selection (two possible pathways)
multi-way selection (many possible
pathways)
• Following is the example flowchart to find
biggest among two numbers
Cont..
Repetition Logic
• Repetition allows for a portion of
an algorithm or computer program
to be executed any number of
times dependent on some
condition being met.
• An occurrence of repetition is
usually known as a loop.
• The termination condition can be
checked or tested at the beginning
or end of the loop, and is known
as a pre-test or post-test,
respectively.
• Flowchart to find factorial of
given no
Pre-test or Post-test
Algorithm
An algorithm is a finite set of instructions, those if followed,
accomplishes a particular task. It is not language specific, we
can use any language and symbols to represent instructions.
The criteria of an algorithm
Input: Zero or more inputs are externally supplied to the
algorithm.
Output: At least one output is produced by an algorithm.
Definiteness: Each instruction is clear and unambiguous.
Finiteness: In an algorithm, it will be terminated after a
finite number of steps for all different cases.
Effectiveness: Each instruction must be very basic, so the
purpose of those instructions must be very clear to us.