Deriving an Algorithm's Runtime Function

Let’s learn how to compute the time complexities of commonly used instructions.

Runtime function of an algorithm

The number of operations performed determines how long an algorithm takes to run for a given input. An algorithm’s running time increases as the number of operations increases and vice versa. We normally want to know how many operations an algorithm will perform. The following are some derivations of runtime functions of algorithms:

Constants

If any line of code is a statement with basic operations, e.g., comparisons, assignments, or reading a variable, they take constant time. Thus, the time complexity of each statement is O(1)O(1).

Let’s look at an example.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.