Professional Documents
Culture Documents
Algorithms: Unit-1
Algorithms: Unit-1
Unit-1
Introduction to an Algorithm
Algorithms are the building blocks of computer programs. They are as important to
programming as recipes are to cooking. An algorithm is a well-defined procedure
that takes input and produces output
The study of algorithms is one of the key foundations of computer science
Day to Day problem solving and making strategies to handle different situations.
Examples :
Watching TV ( How to operate remote in front of TV)
Making a cup of coffee or tea (step by step process to making a coffee)
Plan to go picnic (Sharing money and planning a picnic)
Every day | Daily activities
Now we can say that problem is a kind of state or barrier to achieve something and
problem solving is process to get that state removed by performing some sequence
of activities.
Real Life Algorithms
An Algorithm for making cup of coffee
Algorithm begins with input specification. It has to be described in a precise and accurate
manner.
Inputs for making cup of Coffee
1 cup milk, 1 teaspoon sugar, 1 teaspoon of coffee powder, 1 teaspoon of water.
1. Add 1 teaspoon of coffee powder into the cup
2. Add 1 teaspoon sugar into the cup
3. Add 1 teaspoon of water into the cup
4. Stir the ingredients in the cup until well blended
5. Boil milk for 3 minutes of high flame
6. Add hot milk to the coffee syrup and stir until well blended
7. Finally coffee ready
Output specification
At the end of the execution of these instructions that is one cup of hot coffee is ready to
be served.
What is an Algorithm
Algorithm is a sequence of elementary operations that gives solution to any
problem from a particular specific class of problem in a finite number of
steps.
A Sequence of activities to be processed for getting desired output from given
input.
A formula ( Method or Function ) set of steps for solving a particular
problem. To be an algorithm, a set of rules must be unambiguous and have a
clear stopping point.
There may be more than one way to solve a problem, so there may be more
than one algorithm for a problem.
As per the above definitions we can say that,
1. Getting desired or specified output is essential after algorithm is executed.
2. One will get output only if algorithm stops after finite time.
3. Activities in an algorithm to be clearly defined in other words for it to be
unambiguous
Think Before Writing An Algorithms Why?
write an algorithm you must first understand the function that needs to be
performed very clearly.
If you are not very clear on how to perform the task then you will not be able to
write the algorithm.
Once you are very clear on how to perform the task and state the steps clearly
and precisely you also need to think about what would be the input specification
and the output specification of the algorithm.
Notice that the ingredients of the algorithm are input specification and the
output specification.
Without any of these ingredients the algorithm itself is not complete.
An algorithm is the heart of problem solving in Computer Science and also Real
Life
Informal definition of an algorithm
used in a computer
Before writing an algorithm for a problem, we should find
out to followings .
what is/are the inputs to the algorithm.
what is/are expected output after running the algorithm.
While writing algorithms we will use following symbol for
different operations
„+‟ for Addition
„-‟ for Subtraction
„*‟ for Multiplication
„/‟ for Division
for assignment
„>‟ greater than,‟>=„greater than or equal to
„<„ Less than ,‟<=‟ Less than or equal to
“==“ Equals to ,‟!=„ not Equals to
For example A X*3 means A will have a value of X*3.
Qualities of a good algorithm
Inputs and outputs should be defined precisely.
Each step in algorithm should be clear and unambiguous.
Algorithm should be most effective among many different ways to solve a problem
An algorithm shouldn't have computer code. Instead, the algorithm should be
written in such a way that, it can be used in similar programming languages
Algorithm is not the computer code. Algorithms are just the instruction which
gives clear picture to your idea to write the computer code
We need to make sure that all of these parameters given for a good algorithm are
incorporated when we are actually constructing a good algorithm.
Properties of an algorithm
Input
An algorithm accepts zero or more inputs
Output
It produces at least one output
Definiteness
Each step in algorithm is unambiguous
This means that the action specified by the step cannot be interpreted (it explain the
meaning of step) in multiple ways & can be performed without any confusion
Effectiveness
It consists of basic instructions that are realizable. This means that the instructions can
be performed by using the given inputs in a finite amount of time
Termination
An algorithm terminates after a finite numbers of steps
Advantages and Disadvantages of Algorithm
Advantages
It is a step –by – step representation of a solution to a given problem , which is very
easy to understand
It has produces a definite procedure.
It easy to first develop an algorithm , and then convert it into a flowchart and then
into a computer program.
It is independent of programming language.
It is easy to debug as every step is got its own logical sequence.
An algorithm is important in optimizing a computer program according to the available
resources.
Ultimately when anyone decide to solve a problem through better algorithms then
searching for the best combination of program speed and least amount of memory
consumption is desired
Disadvantages
It is time consuming and cumbersome (Difficult to handle) as an algorithm is
developed first which is converted into flowchart and then into a computer program.
Algorithm Efficiency
Speed or Time
An algorithm that takes the least or minimum time to execute
Space or Memory
An algorithm that uses the least memory or space
Code or programme or set of Instructions
An algorithm is very shortest description to generate the code
Speed or Time is the most important factor to build an efficient algorithm
Write an algorithm to find the area of a Circle using radius r
Note :- previous algorithms are the best examples for sequence algorithm
In flowcharts, sequence of statements is usually contained in the rectangular process box
Branching | Selection | Decision
Algorithm Concepts
IF-THEN-ELSE is decision that is made between two course of actions.
In a conditional statement you make a test.
The result of the test is a Boolean - either True or False.
If the result of the test is True you take a certain course of action
if the result of the test is False you take another course of action.
5.2 Endif
5.3 increment i
Step6: Endwhile
Step 7: End
Write an algorithm to find the factorial of a
number entered by user
Inputs to the algorithm:
Input n (i.e. factorial of given number(6))
Expected output:
Factorial of 6 : - 720 (1*2*3*4*5*6)
Algorithm
Step 1: Start
Step 2: Declare variables n,factorial and I
Step3: Read Value n.
Step4: Set factorial←1
Step4: Set i←1
Step 5: Repeat the steps until i equals to n
5.1 set factorial factorial*i
5.2 set increment i
Step 6: Display factorial
Step 7: End
Write an algorithm to find the factors of a number
entered by user
Inputs to the algorithm:
Input fct (i.e. Enter any number to find its factor(6))
Expected output:
Factors of fct : - 1 2 3 6
Algorithm
Step 1: Start
Step 2: Declare variables fct and i
Step3: Read Value fct.
Step4: Set i←1
Step5: Repeat the steps until i greater than fct
5.1 if fct % i equals to 0 // (remainder of fct/i)
5.1.1 Display i
5.2 Endif
5.3 Set increment i
Step 6: End
In the flowcharts, a back arrow hints the presence of a loop.
A trip around the loop is known as iteration.
You must ensure that the condition for the termination of the looping must
be satisfied after some finite number of iterations, otherwise it ends up as an
infinite loop,
You can use a WHILE loop and a FOR loop interchangeably.
But normally you use a FOR loop when you know the exact number of
iterations of the loop.
You use a WHILE loop when the number of iterations is not known a priori.
You can also nest one loop inside another or a conditional inside a loop or
vice versa. It is best to indent your pseudocode to delineate your blocks of
code.
Nested Control Structures
Combining the use of the control structures with in control Structure
1. a loop within a loop (nested loops)
2. a branch within another branch (nested if)
3. a branch within a loop
4. a loop within a branch
so forth, is not uncommon.
Complex algorithms may have more complicated logic structure and deep
level of nesting, in which case it is best to demarcate parts of the algorithm
as separate smaller modules.
Write an algorithm to find the largest among three different numbers
entered by user
Inputs to the algorithm: Step 1: Start
Input a,b,c Step 2: Declare variables a, b and c.
Expected output: Step 3: Read variables a, b and c.
Display highest value
Step 4: If a greater than b
4.1 If a greater than c
4.1.1 Display a is the largest number.
Else
4.1.2 Display c is the largest number.
4.2 Endif
Else
4.3 If b greater than c
4.3.1 Display b is the largest number.
Else
4.3.2 Display c is the greatest number.
4.4 Endif
Step 5: Endif
Step 6: End
Write an algorithm to check whether a number entered by user is
prime or not
Step 1: Start
Inputs to the algorithm:
Step 2: Declare variables n, i, count.
Enter Checking Number
Expected output: Step 3: Read n from user
Display Prime | Not Prime Step 4: Set count←1
Step 4: Set i←2
Step 5: Repeat the steps until i less than (n/2)
5.1 If remainder of n module i equals to 0
5.1.1 set count←0
5.2 Endif
5.3 Set i←i+1
Step 6: If count equals to 0
6.1 Display n is not prime
else
6.2 Display n is prime
Step 7: Endif
Step 8: End
Write an algorithm to find the Fibonacci series till term≤1000.