You are on page 1of 3

GIFT University, Gujranwala

Lab Manual
for
Data Structure and Algorithms
(CS 204)

Course Instructor Muhammad Hassan Shahid


Lab Instructor(s) Zeeshan Zulfiqar
Date 11/14/18
Semester Fall 2018

Department of Computer Science


GIFT University, Gujranwala, Pakistan
Lab 3
For the following codes carry out the analytical analysis to evaluate time complexity T(n) and
then express it in term of Big Oh.

a. d.
int sum,i;
sum = 0; int sum,i,j;
for (i=0;i<n;i++) sum = 0;
sum++; for (i=1;i<n;i=i*2)
{
T(n) = for(j=0;j<n;++j)
Big O= {
sum++;
b. }
}
int sum,i,j;
sum = 0; T(n) =
for (i=0;i<n;++i) Big O=
{
for(j=0;j<n; ++j) e.
{
sum++; int sum,i,j;
} sum = 0;
} for (i=0;i<n;++i)
{
T(n) = for(j=0;j<=i;++j)
Big O= {
sum++;
c. }
int sum,i; }
sum = 0;
for (i=0;i<n;i=i*2) T(n) =
sum++; Big O

T(n) =
Big O=
Experiment 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 “ctime” library is needed for this
purpose.

#include <iostream>
#include <ctime>
using namespace std;

int main(){
std::clock_t start;
std::clock_t end;
double cpu_time_used;
start=std::clock();
//place your code here
end=std::clock();
cpu_time_used = ((double)(end-start));//CLOCKS_PER_SEC;
cout<<cpu_time_used<<" seconds";
}

Excercise2: Experimental Analysis


You are asked to write a program that the following array of 5 numbers:

Input_Numbers [5] = [10000,50000,100000,500000,1000000]

Each number present in the Input_Number array above represents a value for “n”

 For the codes given in table 1 find the execution time for each value of n given in
the array above.

 Then plot separate graphs for time of execution vs. “n”, for all the value of n given in
 the Input_numbers array.
 Use excel for plotting graph.

You might also like