0% found this document useful (0 votes)
27 views4 pages

(Lab-5 Manual) CS-204-DSA - Time Complexity Analysis

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

(Lab-5 Manual) CS-204-DSA - Time Complexity Analysis

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

GIFT School of Engineering

and Applied Sciences

Spring 2020

CS-204: Data Structures and Algorithms


Lab-5 Manual

Time Complexity Analysis using Big-Oh

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

Task #1: Time Complexity T(n)


For the following codes carry out the analytical analysis to evaluate time complexity T(n) and
then express it in terms of Big Oh:

public void code1(long n){


public void code2(long n){
int a = 5; int a = 5;
int sum = 0; int sum = 0;
for(int i=0 ; i < n ; i++){ for(int i=0 ; i < n ; i++){
if(a<5){ if(a==5){
sum++; sum++;
} }
} }
} }
T(n) = T(n) =
Big O = Big O =
public void code3(long n){ public void code4(long n){
int a = 5; int sum = 0;
int sum = 0; for(int i=0 ; i < n ; i++){
for(int i=0 ; i < n ; i++){ for(int j=0; j < n; j++){
if(a>=5){ for(int k=0; k < n; k++){
a++; sum++;
} }
} }
} }
}
T(n) = T(n) =
Big O = Big O =
public void code5(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++){
for(int j=0; j < i; j++){ for(int j=0; j < i; j++){
for(int k=0; k < j; k+ for(int k=0; k < n; k++){
+){ sum++;
sum++; }
} }
} }
} }
}

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

for(int j=0; j < n; j++){ for(int j=0; j < i; j++){


for(int k=0; k < j; k+ for(int k=0; k < n; k++){
+){ sum++;
sum++; }
} }
} }
} }
}
T(n) =
T(n) = Big O =
Big O =
Table 1

Page 3

You might also like