FDS (Complexity - of - Algorithm)
FDS (Complexity - of - Algorithm)
c = a + b;
S(P)-> O(1)/Constant
printf("%d", c);
}
Example of Space Complexity
▪ Example#1:
#include <stdio.h> Space Complexity Calculation:
int main() 1. Array size -> n
{ 2. Space occupied by the array-> 4 * n = 4n
int n, i, sum = 0; 3. Integer Variable->3 (n, I, sum)
scanf("%d", &n); 4. Size of integers-> 3*4 = 12 bytes
int arr[n]; 5. Total space-> 4n + 12 bytes.
for(i = 0; i < n; i++)
{
S(P) -> O(n) or linear
scanf("%d", &arr[i]);
sum = sum + arr[i];
}
printf("%d", sum);
}
Time Complexity
• What is Time Complexity?
– Time complexity of an algorithm signifies the total time required
by the program to run till its completion.
– Time Complexity can be determined by:
• Size of input data in program
• No. of instructions
• The configuration of machine which is used to execute the
program
• Machine language instruction set
• Translation time required for compiler to convert to machine
language
• Time required for execution of each instruction.
Time Complexity
• Frequency count:
– For same algorithm execution time on faster computer will
be less whereas on slower computer it will be more.
– Hence actual time complexity can be different for each
machine
– The solution for this problem is frequency count.
– Frequency count is a count that denotes the number of
times each instruction in the algorithm is executed.
Time Complexity
• Example #1 of Frequency Count:
Void main()
{ Instruction Freq. Count
int x;
x=25; x=25 1
y=50;
y=50 1
x=x+y;
printf(“%d”, x); x=x+y 1
}
printf(“%d”, x) 1