Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Time Complexity Analysis

Bog-O Notation

 notation is used in Computer Science to describe the performance or


complexity of an algorithm.
 describes the worst-case scenario, and can be used to describe the
execution time required by an algorithm.
O(1)

 describes an algorithm that will always execute in the same time (or space)
regardless of the size of the input data set
O(N)

 describes an algorithm whose performance will grow linearly and in


direct proportion to the size of the input data set
O(N2)

 represents an algorithm whose performance is directly proportional to


the square of the size of the input data set
O(2N)

 denotes an algorithm whose growth doubles with each addition to the


input data set
O(log N)

 denotes an algorithm that will continue to halve the data set with each
iteration until the value has been found or until it can no longer split
the data set.

while (x > 0) while (i < n)


{ {
x/=2; i*=2;
} }
Comparison of Growth Function
Calculate the function time of the given algorithm using O-notation (1)

int addNumber()
{
int a = 5; 1 Unit of time for every operation.
int b = 6; Operation such as arithmetic, relational
int sum; logical or assignment operation.
sum = a+b; Let T be the algorithm
return sum; O(T) = 4
}
Calculate the function time of the given algorithm using O-notation (2)

int addNumber()
{
int a = 5; Ignore the lower order terms.
int b = 6; Ignore the constant multiplier.
int sum;
sum = a+b; Let T be the algorithm
return sum; O(T) = 4
} O(T) = O(1)
Calculate the function time of the given algorithm using O-notation (3)

int n = 5;
int sum = 0;
for(int i = 1; i<=n;i++)
{
sum+=i;
}
Calculate the function time of the given algorithm using O-notation (4)

int n = 5;
int sum = 0;

for(int i=1; i<=n;i++)


{
for(int j=1;j<=n;j++)
{
sum+=i;
}
}
Exercise: Calculate the function time of the given algorithm using O-notation

int n = 5;

for(int i=1; i<n;i++)


{
for(int j=0;j<n;j++)
{
if (j%2==1)
break;
}
}

You might also like