Professional Documents
Culture Documents
Fig1:
What is an ALGORITHM?
An ALGORITHM
is a “sequence of steps designed for programming a computer to solv
e a specific problem.” normally by using machine .
In simple terms
An Algorithm is just the instructions you use to accomplish a goal.
REM A%B
b] Assign the value of B to A
AB
c] Assign the value of remainder to B
BREM
Repeat above 3 steps (a,b,c) as long as the value of B
becomes zero.
There it is you have the GCD in A.
A 8 12(step b) 8
4
B 12 8(step c) 4
0
REM 8(step a) 4 0
PSEUDOCODE REPRESENTATION
ALGORITHM: GCD
Step1:[Accept 2 numbers]
Read A,B
Step2:[compute GCD]
While(b!=0)
{
REM=A%B;
A=B;
B=REM;
}
GCD=A
Step3:[Display result]
Print GCD
Step4:[Finished]
stop
if condition
then
{
S1
-
-
-
Sn
}
if condition
then
{
S1
-
-
-
Sn
}
else
{
s1
-
-
-
sn
}
What is Sorting?
Sorting is a process of arranging the random numbers either in
Ascending or Descending order
Algorithm bubble_SortS (Arr,n)
//Arr is an array of size n
{
for i:=0 to i<=n-1 step i++
{
for j:=0 to j<-n-2-i step j++
{
if Arr[j]>Arr[j+1]
then
{
temp=Arr[j];
Arr[j]=Arr[j+1];
Arr[j+]=temp;
}
}
}
Tricky is it? Ok lets learn the logic used in the above algorithm
Logic of BUBBLE SORT: compare the adjacent numbers . if the number
to the left is greater than the number to the right, then exchange them.
If number to the left is not greater than the number to the right, then
don’t exchange them. This process of comparing has to be repeated
many times
Let us consider an example of an array which has 5 elements (n=5)
you can notice that the largest element 86 has reached the end of the
array(which is the place where it has to be in ascending order).so we
will have to sort the remaining portion of the array.
you can notice that the second largest element 42 has reached the last
but one position of the array(which is the place where it has to be in
ascending order).so we will have to sort the remaining portion of the
array.
Thus, you can notice that the array is sorted in ascending order . you
can also notice that the comparison operation has to be performed
many times . hence the comparison and exchange must be placed
inside the loop.
so let us see the loop again .
this is it. This is the logic of bubble sort.
Both Example1(finding maximum element ) and Example2(bubble sort)
are such methods which produces the expected result when they are
called only once. But, there are few methods which produces the
expected result when they are called RECURSIVELY. For such methods
which should be called recursively we write RECURSIVE ALGORITHMS.
RECURSIVE ALGORITHMS
Algorithm Factorial
//to find the factorial of n
Step1:[Accept n]
Read n;
Step2:[call the function fact()]
result=fact(n);
Step3:[display result]
Write result;
Step4:[finished]
Stop;
Algorithm fact(n)
{
if n==0
then
return 1;
else
return n*fact(n-1);
}
this is the complete algorithm to find the factorial . butt, how would it
work???
Continue reading…
We all know that factorial of 0 is 1 (ie. 0!=1). For all numbers(n) greater
than 0,factorial of n would be n* factorial(n-1). The same logic ,if we
have to depict it in the form of a recurrence relation then, it would go
as follows
lets now consider a number n to be 3 and lets apply the above
algorithm fact(n] to get the factorial (3!)
As , noticed in the above example inorder to find 3! We had to call
fact(n) 3 times. This is one of the few methods which produces the
expected result when they are called RECURSIVELY.And the algorithm
associated with it is a recursive algorithm.
Example 2:Recurssive algorithm for obtaining a solution for TOWER OF
HANOI problem .
Lets now see the recursive algorithm for tower of Hanoi
Algorithm TowerOfHanoi
Step1:[Accept the number of discs]
Read n;
Step2:[call the function tower()]
tower(n,peg1,peg2,peg3);
Step3:[finish]
Stop;
Algorithm tower(n,src,inter,dest)
Thus ,the third disc has been moved from src to dest peg.
3)move the n-1 disc from inter to dest peg using the src peg.
Thus 2 discs (n-1 discs) have been moved from inter to dest peg. During
this movement , the src peg was used.
The above logic in the form of recurrence relation would be-
Analysis Framework
Once the algorithm is built, the next step is to analyze
How efficient the algorithm is ?
In order to analyze the algorithm, we actually consider
two parameters.
*)TIME
*)SPACE
We measure efficiency of an algorithm in terms of how
fast it runs (executes) and we refer to it as time
efficiency.
And we also measure efficiency of an algorithm in
terms of how much extra (more ) space the algorithm
requires to run(executes).
ORDEROF GROWTH
We know that the algorithm works at different speeds
based on the number of inputs provided(n)
But we always expect that the algorithm should work
efficiently for all values of n.
As we have noticed that behavior of the algorithm changes as and
when the value of n increases this change is usually analyzed using
higher value of n
That is the order of growth would usually be determined for higher
number of inputs(n)
The order of growth for few functions with different number of inputs
(n)
n log n n n log n n2 n3 2n n!
21 0.3 21 0.6 4 8 4 2
22 0.6 22 2.4 16 64 16 24
23 0.9 23 7.2 64 512 256 40320
24 1.2 24 19.2 256 4096 65536 20922789888000
25 1.5 25 48.1 1024 32768 4294967296 2.63e35
the above table indicates the running time of the algorithm based on the function
of the algorithm uses
As we can observe in the above table as the value of n increases the value of the
functions are increasing . we refer to this as ORDER OF GROWTH
But, if you still carefully analyze the data with respect to the function 2n and n! as
the input size ( n) increases in a small amount the value obtained is increasing to a
great extent, we refer this to as exponential growth.
The Algorithm running time ofcourse can be measured for a few algorithms by using the functions based
on the number of inputs I,e the value of n but,
For few algorithms the running time is not only dependent on input . because , for the same amount of
inputs the running time(efficiency) of the algorithm can very such algorithms efficiency is measured
using
Consider an example
Algorithm LinearSearch(Arr[0,n-1],key)
do
If key==Arr[i]
then
{
write “elements found @ index I”
the linear search algorithm is a classic example for an algorithm whose efficiency dosenot depend on
number of inputs
The key is compared repeatedly with the array elements till we find the key or till we reach the end of
the array .