Imagine a classroom of 100 students in which you gave your pen to one person. You have to find that pen without knowing to whom you gave it.
Here are some ways to find the pen and what the O order is.
- O(n2): You go and ask the first person in the class if he has the pen. Also, you ask this person about the other 99 people in the classroom if they have that pen and 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 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. Repeat the process till you are left with one student who has your pen. This is what you mean by O(log n).
Is the Time Complexity Same as Time of Execution?
The Time Complexity is not equal to the actual time required to execute a particular code, but the number of times a statement executes.
For 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. For Linux based operating system (Fedora or Ubuntu), use the below commands:
To compile the program: gcc program.c – o program
To execute the program: time ./program
You will get surprising results i.e.:
- For n = 10: you may get 0.5 ms time,
- For n = 10,000: you may get 0.2 ms time maybe because the system was less loaded when you ran this.
- Also, you will get different timings on different machines. Even if you will not get the same timings on the same machine for the same code, the reason behind that is the current load.
So, we can say that the actual time required to execute code is machine-dependent!
What is meant by the Time Complexity of an Algorithm?
- Instead of measuring actual time required in executing each statement in the code, Time Complexity considers how many times each statement executes.
- We measure rate of growth over time with respect to the inputs taken during the program execution.
Example 1: Consider the below simple code to print Hello World
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World";
return 0;
}
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
import java.io.*;
class GFG {
public static void main(String[] args)
{
System.out.print("Hello World");
}
}
print("Hello World")
using System;
public class GFG{
static public void Main (){
// Code
Console.WriteLine("Hello World");
}
}
console.log("Hello World")
Output
Hello World
Time Complexity: In the above code “Hello World” is printed only once on the screen.
So, the time complexity is constant: O(1) i.e. every time a constant amount of time is required to execute code, no matter which operating system or which machine configurations you are using.
Example 2:
#include <iostream>
using namespace std;
int main()
{
// Here n is assumed to be user input
int i, n = 8;
for (i = 1; i <= n; i++) {
cout << "Hello World !!!\n";
}
return 0;
}
#include <stdio.h>
void main()
{
// Here n is assumed to be user input
int i, n = 8;
for (i = 1; i <= n; i++) {
printf("Hello World !!!\n");
}
}
class GFG {
public static void main(String[] args)
{
// Here n is assumed to be user input
int i, n = 8;
for (i = 1; i <= n; i++) {
System.out.printf("Hello World !!!\n");
}
}
}
# Here n is assumed to be user input
n = 8
for i in range(1, n + 1):
print("Hello World !!!")
using System;
public class GFG {
public static void Main(String[] args)
{
// Here n is assumed to be user input
int i, n = 8;
for (i = 1; i <= n; i++) {
Console.Write("Hello World !!!\n");
}
}
}
// Here n is assumed to be user input
let i, n = 8;
for (i = 1; i <= n; i++) {
console.log("Hello World !!!");
}
Output
Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!!
Time Complexity: In the above code “Hello World !!!” is printed only n times on the screen, as the value of n can change.
So, the time complexity is linear: O(n) i.e. every time, a linear amount of time is required to execute code.