Data and File Structure
Data and File Structure
Data and File Structure
Algorithm: an algorithm is a finite set of instruction which, if followed accomplish a particular task.
Every algorithm should have following properties:
It must be correct. In other words, it must compute the desired function, converting each
input to the correct output.
It is composed of a series of concrete steps. Concrete means that the action describes by
that step is completely understood- and doable- by the person or machine that must
perform the algorithm
There can be no ambiguity as to which will be perform next
It must be composed of a finite number of steps
It must terminate. In other words, it may not go into an infinite loop.
Operation counts
Step counts s/e frequency total steps
Algorithm sum (a,n) { 0 0 0
sum = 0; 1 1 1
for(I =1; i<=n; i++); 1 n+1 n+1
sum = sum + a[i]; 1 n n
return sum; 1 1 1
} 0 0 0
total= 2n+3
Ex: Algorithm sum(a,b, c,m,n) { 0 0 0
for(I =1; i<=n; i++) { 1 m+1 m+1
for (j = 1; i<=n; j++) { 1 m(n+1) mn+m
c [i] [j] = a [i] [j] + b [i] [j];1 m(n) mn
}
}
Total: 2mn + 2m +1
= 2 n^2 + 2n +1
If n <=1 then 1
Output ‘n’
Else
F2 = 0;
F1 = 1;
For i = 2 to n do { n
F = f1+ f2; n -1
F2 = f1; n-1
F1 = f; n-1
Output ‘f’ 1
Total : 4n +1
Not that if n <= 1 then the step count is just 2 and 4n +1 otherwise