Time and Space Complexity
Time and Space Complexity
Complexity
• Best case
○ Defines the input for which the algorithm takes the least time
(fastest time to complete).
• Average case
○ Provides a prediction about the running time of the algorithm.
○ Run the algorithm many times, using many different inputs that
come from some distribution that generates these inputs, compute
the total running time (by adding the individual times), and divide
by the number of trials.
○ Assumes that the input is random.
General rules to help us determine the running time of an algorithm.
Loops:
Loops:
Nested Loops:
Consecutive Statements:
If-then-else:
Logarithmic:
Nested Loops:
Square Root:
Cube Root:
Multiple Loops:
Following are the correct definitions of Auxiliary Space and Space Complexity.
Space Complexity of an algorithm is the total space taken by the algorithm with
respect to the input size. Space complexity includes both Auxiliary space and
space used by input.
Time Complexity = O( ? )
Time Complexity = O( ? )
If (x%2==0)
print();
Time Complexity = O( ? )
General rules to help us determine the running time of an algorithm.
Time Complexity = O( ? )
Time Complexity
for( j=1; j <= n; j=j= *O(3) ? ) // j
=j*3
print();
General rules to help us determine the running time of an algorithm.
Time Complexity = O( ? )
for( j=1; j <= n; j++)
for(i=1; i<=j; i++)
print();
Time Complexity = O( ? )