(Lab-5 Manual) CS-204-DSA - Time Complexity Analysis
(Lab-5 Manual) CS-204-DSA - Time Complexity Analysis
Spring 2020
v1.0
4/18/2019
Lab-5 Manual 2019
Performance Analysis
Introduction:
Performance Analysis of Algorithms and Data Structures is carried out in terms of:
1) Space complexity.
2) Time complexity.
Space Complexity of a program is the amount of memory it needs to run to completion.
Time Complexity of a program is the amount of computer time it needs to run to completion.
There are two methods to determine the performance of a program, one is analytical, and the
other is experimental.
Asymptotic Notation:
Asymptotic analysis of an algorithm or data structure refers to defining the mathematical
boundaries of its performance. Using asymptotic analysis, we can conclude the best case,
average case, and worst-case scenario of an algorithm. The BIG-Οh is the formal way to express
the upper bound or worst-case of a program’s time and space complexity.
Analytical Analysis:
Analytically finding time complexity of the following code:
int sum, i, j;
sum = 0;
for (int i = 0 ; i< n ; ++i)
{ for(int j=0 ; j<n ; ++j)
{ sum++;
}
}
Result:
Statement Number of times executed
sum=0 1
i=0 1
i<n n+1
++i N
j=0 N
j<n n(n+1) = n2 + n
++j n2
sum++ n2
Total 3n2+4n+3
T(n) = 3n2+4n+3, T(n) = O(n2)
Page 1
Lab-5 Manual 2019
T(n) =
T(n) = Big O =
Big O =
public void code7(long n){ public void code6(long n){
int sum = 0; int sum = 0;
for(int i=0 ; i < n ; i++){ for(int i=0 ; i < n ; i++){
Page 2
Lab-5 Manual 2019
Page 3