Data Structure-Time & Space Complexity
Data Structure-Time & Space Complexity
Kaustuv Bhattacharjee
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
Introduction
Some Concepts
• Worst Case Running Time: This is the upper bound on the running
time for any input. This means the algorithm will never go beyond the
upper bound.
• Best Case Running Time: This is the lower bound on the running
time for any input. This means the algorithm can never improve
beyond the lower bound irrespective of any input.
Objective:
• Example 1:
for(i=0;i<100;i++)
statement block;
Here 100 is the loop factor. Hence the formula is f(n)=n
• Example 2:
for(i=0;i<100;i+=2)
statement block;
Here the number of iterations is half the number of the loop factor.
Hence the formula is f(n)=n/2
Kaustuv Bhattacharjee, UEM Kolkata
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA
• Example 1:
for(i=0;i<1000;i*=2)
statement block;
for(i=0;i<10;i++)
for(j=0; j<=i; j++)
statement block;
Here the outer loop will be executed 10 times. Inner loop will execute just once in
the first iteration, twice in the second iteration, thrice in the third iteration, so on
and so forth. Hence, the number of iterations can be calculated as
1 + 2 + 3 + ... + 9 + 10 = 55
If we calculate the average of this loop (55/10 = 5.5), we will observe that it is equal
to the number of iterations in the outer loop (10) plus 1 divided by 2.
Big O Notation
Theta(θ) Notation
Thank You