Time and Space Complexity
Time and Space Complexity
• Performance analysis of an algorithm is the process of calculating space required by that algorithm
and time required by that algorithm.
• dataspace
– Time Complexity: Time required to complete the task of that algorithm (Time Complexity
Space Complexity
Space complexity of an algorithm represents the amount of memory space required by the
algorithm in its life cycle.
The space required by an algorithm is equal to the sum of the following two components −
1. A fixed part that is a space required to store certain data and variables, that are
independent of the size of the problem. For example, simple variables and constants
used, program size, etc.
2. A variable part is a space required by variables, whose size depends on the size of the
problem. For example, dynamic memory allocation, recursion stack space, etc.
Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed part and S(I) is the
variable part of the algorithm, which depends on instance characteristic I. Following is a simple
example that tries to explain the
concept −
Algorithm: SUM(A, B)
Step 1 - START
Step 2 - C ← A + B + 10
Step 3 - Stop
Here we have three variables A, B, and C and one constant. Hence S(P) = 1 +3. Now, space depends
on data types of given variables and constant types and it will be multiplied accordingly.
Algorithms with constant space complexity use a fixed amount of memory that does not
depend on the input size.
Linear space complexity implies that the memory usage grows linearly with the size of the
input.
Time Complexity
When we calculate time complexity of an algorithm, we consider only input data and ignore
the remaining things, as they are machine dependent
int
1 unit of time to calculate a+b .
sum(int a, int b)
{ return a+b; 1 unit of time to return the value.
}
Total 2 units of time to complete its execution
An algorithm is said to have constant time with order O (1) when it is not dependent on the input
size n. Irrespective of the input size n, the runtime will always be the same.
While(i<=5)
Printf(“%d”,i);
++i;
An algorithm is said to have a linear time complexity when the running time increases linearly with
the length of the input. When the function involves checking all the values in input data, with this
order O(n).
Example: Int 1=1;
While(i<=N)
Printf(“%d”,i);
++i;
An algorithm is said to have a logarithmic time complexity when it reduces the size of the input data
in each step. This indicates that the number of operations is not the same as the input size. The
number of operations gets reduced as the input size increases. Algorithms are found in binary
trees or binary search functions.
While(i<=N)
Printf(“%d”,i);
i=i*3;
An algorithm is said to have a non-linear time complexity where the running time increases non-
linearly (n^2) with the length of the input. Generally, nested loops come under this order where one
loop takes O(n) and if the function involves a loop within a loop, then it goes for O(n)*O(n) = O(n^2)
order.
Example: Int 1=1;
While(i<=N)
For(int j=1;j<=N;++j)
Printf(“%d”,i);
++i;