Algorithm, ADT and Arrays
Algorithm, ADT and Arrays
• An “algorithm" is a formally defined procedure for performing some calculation. It provides a blueprint to write a
program to solve a particular problem.
• It is considered to be an effective procedure for solving a problem in finite number of steps. That is, a well-defined
algorithm always provides an answer and is guaranteed to terminate.
• Algorithms are mainly used to achieve software re-use. Once we have an idea or a blueprint of a solution, we can
implement it in any high level language like C, C++, Java, so on and so forth.
• Similarly, space complexity of an algorithm is the amount of computer memory required during the program
execution, as a function of the input size.
Calculating Algorithm Efficiency
• The efficiency of an algorithm is expressed in terms of the number of elements that has to be processed. So, if n is the
number of elements, then the efficiency can be stated as Efficiency = f(n).
• If a function is linear (without any loops or recursions), the efficiency of that algorithm or the running time of that
algorithm can be given as the number of instructions it contains.
• If an algorithm contains certain loops or recursive functions then its efficiency may vary depending on the number of
loops and the running time of each loop in the algorithm.
Minimum to
maximum
Calculating Algorithm Efficiency
Linear loops
for(i=0; i<n; i++)
statement block
f(n) = n or Order of n
Logarithmic Loops
for(i=1;i<1000;i*=2) for(i=1000;i>=1;i/=2)
statement block; Iterates 9 times statement block;
log21000 = 9.96 approx. 9
f(n) = log n or Order of log n
Nested Loops
Linear logarithmic
for(i=0;i<10;i++) for(i=0; i<n; i++)
for(j=1; j<1000;j*=2) for(j=0; j<n; j++)
statement block; statement block;
for(i=0; i<n; i++) for(j=0; j<n; j++) for(i=0; i<n; i++) for(j=0; j<n; j++)
read matrix A[i][j]; read matrix A[i][j];
for(i=0; i<n; i++) for(j=0; j<n; j++) for(i=0; i<n; i++) for(j=0; j<n; j++)
read matrix B[i][j]; read matrix B[i][j];
for(i=0; i<n; i++) for(j=0; j<n; j++) for(i=0; i<n; i++) for(j=0; j<n; j++)
C[i][j] = A[i][j] + B[i][j]; C[i][j] = 0;
for(k=0;K<n;k++)
C[i][j] = C[i][j] + A[i][K]*B[k][j];
for(i=0; i<n; i++) for(j=0; j<n; j++)
print matrix C[i][j];
for(i=0; i<n; i++) for(j=0; j<n; j++)
print matrix C[i][j];
func factorial(n)
int i, fact=1; Time Complexity is Order of n
for(i=1;i<=n; i++) Recurrence Relation
fact=fact*i;
return fact; T(n) = a, n=0 or n=1
T(n) = T(n-1) +b n>1
where a and b are constants
99 67 78 56 88 90 34 85