Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

What Is Time Complexity?: Produce There Required Output

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

What is Time Complexity?

Time Complexity of any algorithm/program is not the measure of actual time taken for the program
to be executed, rather it is the number of times each statement of the logic gets executed to
produce there required output.
In simpler terms, here is what we mean. Let us consider the below code..

So, the above code when executed using a compiler has given the below output. If you can see, the
compiler shows that the code was executed in 1.166 secs and a lot of us assume this to be time
complexity, but it isn't.
Rather, the time complexity of this code is dependent on the number of time the statements get
executed. Here, the for loop gets executed 'n' number of times and hence complexity is O(n)
Why do you need to Calculate Time Complexity?

A lot of times, there is more than one way to solve a particular problem and, in such cases, you should be able
to identify the most efficient solution out of all. This is where time complexity comes into the picture. Time
complexity helps you compare the performance of different solutions, thus helping you determine the most
efficient solution.
How do you Calculate the Time Complexity of an Algorithm?

Time complexity can be expressed using the below three terms called as Asymptotic Notations.

 Big - Oh or Big O Notation (BIG O)


 Big - Omega
 Big - Theta

But most times, we will use the Big O notation because it will give us an upper limit of the
execution time i.e. the execution time in the worst case inputs. Also, an algorithm's running time
may vary among different inputs of the same size, and hence we consider worst case complexity,
which is the maximum amount of time required for inputs of a given size.
Note: Time Complexity is nothing but a function of the input size

Join Our Telegram Group to Get Notifications, Study Materials, Practice test & quiz:
https://t.me/ccatpreparations Visit: https://ccatpreparation.com
Example :1

Time Complexity Calculation: The time complexity of the above-given program is O(1), as this program
consists of only assignment, arithmetic operations and all those will be executed only once.

Example :2

Time Complexity Calculation: In the above-given snippet, we have a control statement which
executes for n times. Along with that we also have operations like assignment, arithmetic and a
return statement. Hence, the time complexity is O(n + 3).
For larger values of n, the constant values become negligible. So if a program consists of a control
statement, then the complexities of assignment, arithmetic, logical and return statements can be
ignored.

Hence, the final time complexity of the above-given snippet is O(n).


Example :3

Time Complexity Calculation: In the above snippet, the first & the second for loops get executed n times
individually. So the time complexity accounts to n*n = O(n2)

Join Our Telegram Group to Get Notifications, Study Materials, Practice test & quiz:
https://t.me/ccatpreparations Visit: https://ccatpreparation.com
Example :4

Time Complexity Calculation: This is the algorithm of binary search. It breaks the given set of elements
into two halves and then searches for a particular element. Further, it keeps dividing these two halves into
further halves until each individual element is a set. All such algorithms which work on the principle of
recursive division of elements into halves will have a O(Log n) complexity.

Time Complexity Table

Big O Notation Time Complexity Details

Constant Time Complexity O(1) occurs when the program doesn’t contain any loops,
recursive functions or call to any other functions. The run time, in this case, won’t change no
O(1) matter what the input value is.

Linear Time Complexity O(n) occurs when the run time of the code increases at an order of
O(n)  magnitude proportional to n. Here n is the size of the input.

Logarithmic Time Complexity O(log n) occurs when at each subsequent step in the algorithm,
the time is decreased at a magnitude inversely proportional to N. This generally happens in
O(log n) Binary Search Algorithm.

Linearithmic Time Complexity. One example of an algorithm that runs with this time
O(n log n) complexity is Quick Sort, Heap Sort, Merge Sort

Quadratic Time Complexity


O(n²)

Join Our Telegram Group to Get Notifications, Study Materials, Practice test & quiz:
https://t.me/ccatpreparations Visit: https://ccatpreparation.com
Exponential Time Complexity
O(2^n)

O(n!) Factorial Time Complexity

Time Complexity Cheat Sheet of Algorithms

The below Time Complexity cheat sheet has the best, average and worst-case complexities of some
of the important Data Structure Algorithms.

Worst Case Complexity


Algorithm Best Case Complexity Average Case Complexity

O(n²)
Bubble Sort O(n)  O(n²)

O(n²)
Selection Sort O(n²) O(n²)

O(n²)
Insertion Sort O(n) O(n²)

O(n Log(n))
Merge Sort O(n Log(n)) O(n Log(n))

O(n²)
Quick Sort O(n Log(n)) O(n Log(n))

O(n Log(n))
Heap Sort O(n Log(n)) O(n Log(n))

Radix Sort O(nk) O(nk) O(nk)

Join Our Telegram Group to Get Notifications, Study Materials, Practice test & quiz:
https://t.me/ccatpreparations Visit: https://ccatpreparation.com

You might also like