Computing the running time complexity of an algorithm
To analyze an algorithm with respect to the best-, worst-, and average-case runtime of the algorithm, it is not always possible to compute these for every given function or algorithm. However, it is always important to know the upper-bound worst-case runtime complexity of an algorithm in practical situations; therefore, we focus on computing the upper-bound Big O notation to compute the worst-case runtime complexity of an algorithm:
- Find the worst-case runtime complexity of the following Python snippet:
# loop will run n times for i in range(n): print("data") #constant time
Solution: The runtime for a loop, in general, takes the time taken by all statements in the loop, multiplied by the number of iterations. Here, total runtime is defined as follows:
T(n) = constant time (c) * n = c*n = O(n)
- Find the time complexity of the following Python snippet: ...