0% found this document useful (0 votes)
187 views5 pages

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

This document provides instructions for a lab manual on analyzing the time complexity of algorithms using Big-O notation. It discusses performing time complexity analysis both analytically and experimentally. Students are asked to calculate the time complexity of various codes analytically and experimentally by running the codes on different input sizes and measuring execution time. They are then instructed to plot graphs of execution time versus input size to verify that the results match the theoretical time complexities.

Uploaded by

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

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

This document provides instructions for a lab manual on analyzing the time complexity of algorithms using Big-O notation. It discusses performing time complexity analysis both analytically and experimentally. Students are asked to calculate the time complexity of various codes analytically and experimentally by running the codes on different input sizes and measuring execution time. They are then instructed to plot graphs of execution time versus input size to verify that the results match the theoretical time complexities.

Uploaded by

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

GIFT School of Engineering

and Applied Sciences

Fall 2019

CS-242: Data Structures and Algorithms

Lab-4 Manual
Time Complexity Analysis using Big-Oh

v1.0
4/4/2019
Lab-4 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-4 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 sum,i; int sum,i,j;
sum=0; sum=0;
for(i=0 ; i<n ; ++i){ for(i=1 ; i<n ; i=i*2){
sum++; sum++;
} }
} }

T(n) = T(n) =
Big O = Big O =
public void Code3(long n){ public void Code4(long n){
int sum,i,j; int sum,i,j;
sum = 0; sum = 0;
for (i=1;i<n; ++i) for (i=0;i<n;++i)
{ {
for (j=0;j<i;++j) for (j=0;j<n;++j)
{ sum++; { sum++;
} }
} }
} }
T(n) = T(n) =
Big O = Big O =
public void Code6(long n){
public void Code5(long n){ int sum,i,j;
int sum,i,j; sum = 0;
sum = 0; for (i=1;i<n; i=i*2)
for (i=1;i<n; i=i*2) {
{ for (j=0;j<n;++j)
for (j=0;j<i; j++) {sum++;
}
{ sum++; }
} }
}
}
T(n) = T(n) =
Big O = Big O =
Table 1

Page 2
Lab-4 Manual 2019

Experimental Analysis:
To find time complexity of a code experimentally we need to calculate the time difference
between starting and ending time of execution of code. So to take the time in java we use
System.currentTimeMillis()method which will return us the current time of type
long in milliseconds.

We will use Thread.sleep(1000) method to make an delay of one second in our program.
Thread.sleep() requires time in milliseconds of type long. Where 1 second is equal to
1000 milliseconds so Thread.sleep(1000) will make an pause of 1 second in program.

Code mentioned below is used to calculate the actual running time of the Code which maybe
different on every computer due to their processor and clock speed.

public class TimeTest {

public static void main(String [] args) throws


InterruptedException{

long startTime;

startTime = System.currentTimeMillis();

//Your Code Here

Thread.sleep(1000);

for(int i=0;i<100000;i++){}

long endTime;

endTime = System.currentTimeMillis();

long totalTime = (endTime - startTime);

System.out.println("This Code is Taking : "+totalTime +"


Milli seconds to Run !");

}//main

}//TimeTest

NOTE: Copy the TimeTest.java code from the Code folder then compile and run the code.

Page 3
Lab-4 Manual 2019

Task #2: Time Calculation


In this task you are required to calculate the execution time of those program on which you perform
analytical analysis in Task 1.
Write a program that takes the following array of 5 numbers:
long inputArray [5] = [10000, 50000, 100000, 500000, 1000000];
Each number present in the inputArray above represents a value for “n” in Table1 of Task1.
Now you are required to do things listed below
1. For the codes given in Table1 of Task1 find the execution time for each value of n given
in the array above.
2. Then plot separate graphs for time of execution vs. “n”, for all the values of n given in the
inputArray array.
3. Use excel for plotting graph
4. Verify that the graphs you plotted matches the asymptotic notation shown in Figure 1.

Figure 1

Page 4

You might also like