Lot of students get confused while understanding the concept of time-complexity, but in this article we will explain it with a very simple example:
Imagine a classroom of 100 students in which you gave your pen to one person. Now, you want that pen. Here are some ways to find the pen and what the O order is.
O(n2): You go and ask the first person of the class, if he has the pen. Also, you ask this person about other 99 people in the classroom if they have that pen & So on,
This is what we call O(n2).
O(n): Going and asking each student individually is O(N).
O(log n): Now I divide the class in 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. Repeat the process till you are left with one student who has your pen. This is what you mean by O(log n).
I might need to do the O(n2) search if only one student knows on which student the pen is hidden. I’d use the O(n) if one student had the pen and only they knew it. I’d use the O(log n) search if all the students knew, but would only tell me if I guessed the right side.
Time Complexity of algorithm/code is not equal to the actual time required to execute particular code but the number of times a statement execute. We can prove this by using time command. For example, Write a code in C/C++ or any other language to find maximum between N numbers, where N varies from 10, 100, 1000, 10000. And compile that code on Linux based operating system (Fedora or Ubuntu) with below command:
gcc program.c –o program run it with time ./program
You will get surprising results i.e. for N = 10 you may get 0.5ms time and for N = 10, 000 you may get 0.2 ms time. Also you will get different timings on different machine. So, we can say that actual time require to execute code is machine dependent (whether you are using pentium1 or pentiun5) and also it considers network load if your machine is in LAN/WAN. Even you will not get same timings on same machine for same code, the reason behind that the current network load.
Now, the question arises if time complexity is not the actual time require executing the code then what is it?
The answer is : Instead of measuring actual time required in executing each statement in the code, we consider how many times each statement execute.
In above code “Hello World!!!” print only once on a screen. So, time complexity is constant: O(1) i.e. every time constant amount of time require to execute code, no matter which operating system or which machine configurations you are using.
Now consider another code:
Hello Word !!!Hello Word !!!Hello Word !!!Hello Word !!! Hello Word !!!Hello Word !!!Hello Word !!!Hello Word !!!
In above code “Hello World!!!” will print N times. So, time complexity of above code is O(N).
Source : Reddit
The co-author of this article is Varsha Lokare.
- A Time Complexity Question
- An interesting time complexity question
- Time Complexity of Loop with Powers
- Time Complexity of building a heap
- Practice Questions on Time Complexity Analysis
- Time Complexity where loop variable is incremented by 1, 2, 3, 4 ..
- Time complexity of recursive Fibonacci program
- Time Complexity Analysis | Tower Of Hanoi (Recursion)
- Python Code for time Complexity plot of Heap Sort
- Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially
- Cyclomatic Complexity
- What does 'Space Complexity' mean?
- Knowing the complexity in competitive programming
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Measure execution time with high precision in C/C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.