Examples of time complexity
Let's now examine some examples of time complexity calculations, since in 99% of the cases we need to know the maximum time a function might take to execute; we will be mostly analyzing the worst case time complexity, that is, the upper bound of the rate of growth based on the input of a function.
Constant time
A constant time function is one which takes the same amount of time to execute, irrespective of the size of the input that is passed into the function:
function square(num) { return num*num; }
The preceding code snippet is an example of a constant time function and is denoted by O(1). Constant time algorithms are the most sought out algorithms for obvious reasons, such as them running in a constant time, irrespective of the size of the input.Â
Logarithmic time
A Logarithmic time function is one in which the time of execution is proportional to the logarithm of the input size. Consider the following example:
for(var i = 1; i < N; i *= 2) { // O(1) operations...