Some Fundamental Algorithms
Swapping
• Problem
Given two variables, a and b exchange the values
assigned to them
Let a = 10 and b = 20 (Initial configuration)
Want a = 20 and b = 10 (Target configuration)
Assignment operator “=“ can be used.
a=b causes copying of the value stored in a into b
If we do a=b and b=a what happens?
Swapping
Can use a temporary variable t.
Now,
t=a # t now gets the value of a
a=b # a gets the value of b
b=t # b gets the value in t i.e. the original value of a
Counting
• Problem
Given a set of n students who have marks in the
range (0,100) in a course. The pass marks in the
course are 50. Find how many students have
passed the course.
For one student:
Input marks of this student
If the marks of this student > passmarks
The count of students passed = 1
Otherwise
The count of students passed = 0
Counting
• Problem
Given a set of n students who have marks in the
range (0,100) in a course. The pass marks in the
course are 50. Find how many students have
passed the course.
For one student:
Input marks of this student
If the marks of this student > passmarks
The count of students passed = 1
Otherwise
The count of students passed = 0
Counting
For n students:
Need to repeat the process and update the count.
This can be achieved by using a loop construct such as while.
count = 0 # initiallly the count of students who have passed is 0
m = 0 # m is a variable that gives the number of students for
# whom marks have been processed
while (m < n) do
m = m+1
input marks of the current student
If marks >= 50 count = count +1
end-while
output count
Summation
• Problem
Given a set of n numbers find the sum of these
numbers.
Let the n numbers be a1, a2, a3, …, an
Resulting sum = a1+a2+a3+…+an
For first two numbers sum = a1+a2
The third number a3 can be added into the sum
sum = sum + a3 # update of sum
Similarly for next number a4
sum = sum + a4
Summation
sum = 0 # initial value of sum
input n # total number of number to be added
i = 0 # loop index
while (i<n) do
i=i+1
input ai
sum = sum + ai
end-while
output sum
Factorial
• Problem
Given a number n, compute n factorial (n!) where
n>=0
We know n!=1x2x3x…x(n-1)xn
0!=1
1!=1
2!=1x2
3!=1x2x3
n! can be computed as nx(n-1)!
Factorial
input n
factor=1
for i=1 to n do
factor = i*factor
output factor
Sine function as series
• Problem
Evaluate sin(x) as a series expansion i.e, upto n
terms
Sine function as series
• Problem
Evaluate sin(x) as a series expansion i.e
i.e, upto n
terms
One can observe:
Sine function as series
Current term can be computed using the previous
term.
current_term (ith term) = previous_term*x*x/i*(i-1)
Change of sign can be simply done
sign = -sign
Also next I is obtained by incrementing by 2
i=i+2
The rest is summation of a series to sum to n terms.
Fibonacci series
• Problem
Generate and print first n terms of Fibonacci
sequence, which looks like
0, 1, 1, 2, 3, 5, 8, 13 ….
First two terms are given.
Third term = second term + first term
Forth term = third term + second term
In general
Current term = sum of the two previous terms
Fibonacci series
Initially Next time
a=0 a=b
b=1 b=c
c = a+b c = a+b (New)
This can continue so the algorithm may look like
input n
a=0
b=1
i=2 # keeps track of number of terms decided
while i<n do
i=i+1
c=a+b
a=b
b=c
end-while
Fibonacci series
Initially Next time
a=0 a=b
b=1 b=c
c = a+b c = a+b (New)
This can continue so the algorithm may look like
input n
a=0
b=1
i=2 # keeps track of number of terms decided
while i<n do
i=i+1
c=a+b Any improvement possible?
a=b
b=c
end-while