time compexity
time compexity
By:
Dr. Sushil Kumar,
Assistant Professor
NIT Warangal
Data Structures
Asymptotic Notations: Big-oh, Big-omega, Theta
Algorithms
Asymptotic Notations
Types of Data Structures
Data structure is a particular way of
storing and organizing data in a
computer so that it can be used efficiently.
Specification of
Input
Output
Specification
of Input
Algorithms Output
Time
Memory
Iterative Recursive
Complexity Calculation
Iterative:
fun()
{
for( i = 1 to n )
max (a,b,c);
.
.
.
}
Complexity Calculation
Recursive
fun()
{
if( )
fun(n/2);
}
Complexity Calculation
Any iterative algorithm can be
converted into recursive and any
recursive algorithm can be converted
into iterative.
Analysis: for recursion A(n) and we
are calling A(n/2) inside A(n) then we
will do with f(n) and f(n/2).
In the iterative calculate on the basis
of no. of iterations.
Complexity Calculation
A()
{ int i;
for( i = 1 to n )
cout<<“NITW”;
}
For i = 4 For i = n
For i = 5
J = 16 times j = 𝒏𝟐 time
J = 25 times
K = n/2*16 K = n/2* 𝒏𝟐
K = n/2*25
times times
For i = 1, 2, 4, …n
= 2 ,2 ,2 ,…𝑛
0 1 2
= O(𝑛(log 2 𝑛)^2)
Complexity for Recursive
A(n)
{
if (condition)
return(A(n/2)+A(n/2));
}
=𝒄 𝟐 𝒌+𝟏
−𝟏
= c(2n-1)
= O(n)
Complexity for Recursive
T(n) = 2.T(n/2) + n; n>1
=1; n=1
Complexity for Recursive
n
T (n/2) T (n/2)
(n/2) (n/2)
stack and queue designed for LIFO / FIFO direct access, searching and sorting
size-
Size of each element.