
Since Tsum is in order of n 2, therefore Time Complexity = O(n 2).

Matrix of size n*n => Tsum = a.n 2 + b.n + c.Find the sum of all elements of a matrixįor this one, the complexity is a polynomial equation (quadratic equation for a square matrix) Therefore, the time complexity of the above code is O(n) Therefore the total cost to perform sum operation Instead of measuring actual time required in executing each statement in the code, Time Complexity considers how many times each statement executes.Įxample 1: Consider the below simple code to print Hello World Now, the question arises if time complexity is not the actual time required to execute the code, then what is it? What is meant by the Time Complexity of an Algorithm? So, we can say that the actual time required to execute code is machine-dependent (whether you are using Pentium 1 or Pentium 5) and also it considers network load if your machine is in LAN/WAN. Even if you will not get the same timings on the same machine for the same code, the reason behind that is the current network load. Also, you will get different timings on different machines.For N = 10,000: you may get 0.2 ms time.To compile the program: gcc program.c – o program For Linux based operating system (Fedora or Ubuntu), use the below commands: We can prove this by using the time command.įor example: Write code in C/C++ or any other language to find the maximum between N numbers, where N varies from 10, 100, 1000, and 10000. The Time Complexity of an algorithm/code is not equal to the actual time required to execute a particular code, but the number of times a statement executes. Is the Time Complexity of an Algorithm/Code the same as the Running/Execution Time of Code? NOTE: We are interested in the rate of growth over time with respect to the inputs taken during the program execution. There are other asymptotic notations like theta and Omega. The above O -> is called Big – Oh which is an asymptotic notation. The O(log n) search if all the students knew, but would only tell me if I guessed the right side.The O(n) if one student had the pen and only they knew it.The O(n 2) searches if only one student knows on which student the pen is hidden.Repeat the process till you are left with one student who has your pen. O(log n): Now I divide the class into two groups, then ask: “Is it on the left side, or the right side of the classroom?” Then I take that group and divide it into two and ask again, and so on.O(n): Going and asking each student individually is O(N).Also, you ask this person about the other 99 people in the classroom if they have that pen and so on, O(n 2): You go and ask the first person in the class if he has the pen.
.png)
Here are some ways to find the pen and what the O order is.

Height(currNode) = max(height(currNode.left), height(currNode.right)) + 1.īase condition would be, if the currNode is empty. We can use recursion to solve this problem.maxHeight+1) for the height of current node. In order to find height at each node, we can find height of left subtree and right subtree and take max between these two.If at every node we could find the max height and return that till the root node, that should be the height of the root node i.e.
