EHB208E 3 Lesson
EHB208E 3 Lesson
BLG221E
Algorithm
s
EHB208E
ALGORITHMS AND FLOWCHARTS
• Implementation phase
– implement using a programming language
EXAMPLE
How do you print the first non-repeated character from a
string?
EXAMPLE
How can a given string be reversed using recursion?
EXAMPLE
1.How do you check if a string contains only digits?
Summation Sorting
Product
Smallest and
largest
Summation Algorithm
One commonly used algorithm in computer science is
summation. We can add two or three integers very easily,
but how can we add many integers?
The solution is simple: we use add operator in a loop.
A summation algorithm
has three logical parts:
View Answer
Answer: a
Explanation: The steps performed while running insertion sort on given
array are:
Initial : 9 7 4 2 1 key = 7
7 9 4 2 1 key = 4
4 7 9 2 1 key = 2
2 4 7 9 1 key = 1
12479
Merge Sort Algorithm
• To sort an array A[p . . r]:
• Divide
– Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each
• Conquer
– Sort the subsequences recursively using merge sort
– When the size of the sequences is 1 there is nothing
more to do
• Combine
– Merge the two sorted subsequences
Example – Size of the list is Power of
2
1 2 3 4 5 6 7 8
Divide 5 2 4 7 1 3 2 6 q=4
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
Example – Size of the list is Power of 2
1 2 3 4 5 6 7 8
Conquer 1 2 2 3 4 5 6 7
and Merge
Merge algorith
1 2 3 4 5 6 7 8 m is
2 4 5 7 1 2 3 6 called
1 2 3 4 5 6 7 8
2 5 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
Example – Size of the list is Not a Power of 2
1 2 3 4 5 6 7 8 9 10 11
Divide 4 7 2 6 1 4 7 3 5 2 6 q=6
1 2 3 4 5 6 7 8 9 10 11
q=3 4 7 2 6 1 4 7 3 5 2 6 q=9
1 2 3 4 5 6 7 8 9 10 11
4 7 2 6 1 4 7 3 5 2 6
1 2 3 4 5 6 7 8 9 10 11
4 7 2 6 1 4 7 3 5 2 6
1 2 4 5 7 8
4 7 6 1 7 3
Example – Size of the list is Not a Power of 2
1 2 3 4 5 6 7 8 9 10 11
Conquer
and 1 2 2 3 4 4 5 6 6 7 7
Merge
Merge algorith
1 2 3 4 5 6 7 8 9 10 11
m is
1 2 4 4 6 7 2 3 5 6 7 called
1 2 3 4 5 6 7 8 9 10 11
2 4 7 1 4 6 3 5 7 2 6
1 2 3 4 5 6 7 8 9 10 11
4 7 2 1 6 4 3 7 5 2 6
1 2 4 5 7 8
4 7 6 1 7 3
Merging Algorithm
14 23 45 98 6 33 42 67
Merging Algorithm
14 23 45 98 6 33 42 67
Merge
Merging Algorithm
14 23 45 98 6 33 42 67
Merge
Merging Algorithm
14 23 45 98 6 33 42 67
6 14
Merge
Merging Algorithm
14 23 45 98 6 33 42 67
6 14 23
Merge
Merging Algorithm
14 23 45 98 6 33 42 67
6 14 23 33
Merge
Merging Algorithm
14 23 45 98 6 33 42 67
6 14 23 33 42
Merge
Merging Algorithm
14 23 45 98 6 33 42 67
6 14 23 33 42 45
Merge
Merging Algorithm
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
Merge
Merging Algorithm
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
Merge
Searching Algorithm
Another common algorithm in computer science is
searching, which is the process of finding the location
of a target value among a list of objects.
This
This determines
determines
whether
whether the the target
target is is
in
in the
the first
first half
half oror the
the
second
second halfhalf of
of the
the list.
list.
If
If it
it is
is in
in the
the first
first half,
half,
there
there is is no
no need
need to to
further
further check
check the
the
second
second half.half. If
If it
it is
is inin
the
the second
second half,
half, there
there
is
is no
no need
need to to further
further
check
check thethe first
first half.
half.
In
In other
other words,
words, we we
eliminate
eliminate half half the
the listlist
from
from further
further
consideration.
consideration.
SUB-ALGORITHMS
The three programming constructs (sequence, repetition and
desicion) allow us to create an algorithm for any solvable problem.
An algorithm is defined
This solution usually
recursively whenever
involves a loop.
the algorithm appears
within the definition
itself.
Recursion
• In some problems, it may be natural to define the
problem in terms of the problem itself.
• Recursion is useful for problems that can be
represented by a simpler version of the same
problem.
Example:
• the factorial function
6! = 6 * 5 * 4 * 3 * 2 * 1
We could write:
6! = 6 * 5!
Simpler version of the same problem
Example: factorial function
In general, we can express the factorial
function as follows:
n! = n * (n-1)!
Is this correct? Well… almost.
n! = 1 (if n is equal to 1)
n! = n * (n-1)! (if n is larger than 1)
Factorial function
For certain problems (such as the factorial
function), a recursive solution often leads to short
and elegant code. Compare the recursive solution
with the iterative solution:
Recursive solution Iterative solution
int fac (int numb) {
int fac (int numb){
if(numb<=1) int product=1;
return 1;
else while(numb>1) {
return numb*fac(numb- prod
1); uct *= numb;
}
numb--; }
return product;
}
Recursion
We have to pay a price for recursion:
calling a function consumes more time and memory
than adjusting a loop counter.
High performance applications (graphic action games,
simulations of nuclear explosions) hardly ever use
recursion.
In less demanding applications recursion is an
attractive alternative for iteration (for the right
problems!)
Iteration
If we use iteration, we must be careful not to
create an infinite loop by accident:
Oops!
No
terminatio
n condition
Predict the output of following program. Consider the
following recursive function fun(x, y). What is the value of
fun(4, 3)
View Answer
Answer: a
What does the following function print for n = 25?
void fun(int n) {
if (n == 0)
return;
printf("%d", n%2);
fun(n/2);
}
a.11001
b.10011
c.11111
d.00000
View Answer
Answer: b