Introduction to Algorithms
What is an Algorithm?
An Algorithm is a step-by-step procedure for solving a given
problem.
Example: How to make a cup of tea?
Example: How to withdraw money from ATM
Cont…
• The word Algorithm means “a process or set of rules to be followed in
calculations or other problem-solving operations”.
• Therefore Algorithm refers to a set of rules/instructions that step-by-step
define how a work is to be executed upon in order to get the expected results.
• The Algorithm designed are language-independent, i.e. they are just plain
instructions that can be implemented in any language, and yet the output will
be the same, as expected.
Characteristics of an Algorithm
Source: geeksforgeeks.org
Cont.…
➢Clear and Unambiguous: Algorithm should be clear and unambiguous. Each of its
steps should be clear in all aspects and must lead to only one meaning.
➢Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined
inputs.
➢Well-Defined Outputs: The algorithm must clearly define what output will be
yielded and it should be well-defined as well.
➢Finite-ness: The algorithm must be finite, i.e. it should not end up in an infinite loops
or similar.
➢Feasible: The algorithm must be simple, generic and practical, such that it can be
executed upon will the available resources. It must not contain some future
technology, or anything.
➢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 same, as expected.
Cont.…
Advantages of Algorithms:
➢It is easy to understand.
➢Algorithm is a step-wise representation of a solution to a given problem.
➢In Algorithm the problem is broken down into smaller pieces or steps hence,
it is easier for the programmer to convert it into an actual program.
Disadvantages of Algorithms:
• Writing an algorithm takes a long time so it is time-consuming.
• Branching and Looping statements are difficult to show in Algorithms.
Cont.…
The general form of an algorithm is:
Algorithm:<Algorithm_Name>(input1, input2)
Step 1 : START
Step 2 : <….>
Step 3 : <…..>.
........
Step n : STOP
Find sum of two numbers
Algorithm: SUM(A,B)
Step 1: Start
Step 2: Read values A and B
Step 3: Add A and B
sum = A + B
Step 4: print sum
Step 5: Stop
Find Area of Rectangle
Algorithm: Area_of_Rectangle(length, breadth)
Step 1: Start
Step 2: Read values length and breadth
Step 3: Find area of rectangle
area = length * breadth
Step 4: print area
Step 5: Stop
Finding square of a given number
Algorithm: square(num)
Step 1: Start
Step 2: Read num
Step 3: Find square of num
res = num * num
Step 4: print res
Step 5: Stop
Finding Biggest of two numbers
Algorithm: Big(a, b)
Step 1: Start
Step 2: Read values a and b
Step 3: If a>b then
print “a is the largest number”
Otherwise
print “b is the largest number”
End if
Step 4: Stop
Finding sum of First N natural numbers
Algorithm: sum_of_N_number(N)
Step 1: Start
Step 2: Read N
Step 3: set I=1
Step 4: set sum=0
Step 5: repeat loop until N>=I
sum=sum + I
I=I+1
End loop
Step 6: print sum
Step 7: Stop
Finding square of First N natural numbers
Algorithm: square_of_N_number(N)
Step 1: Start
Step 2: Read N
Step 3: set I=1
Step 4: repeat loop until N>=I
res=I * I
print res
I=I+1
End loop
Step 5: Stop
Try it Yourself
• Write a algorithm to find area of circle.
• Write a algorithm to find area of triangle.
• Write a algorithm to find product of two numbers.
• Write a algorithm to check the given number is even or odd.
• Write a algorithm to print first N numbers.