34
loading...
This website collects cookies to deliver better user experience
1 second
how many similar operation could be performed in O(n)
, O(n^2)
,O(n^3)
and O(2^n)
. O(n)
time.O(N)
algorithm?void bigOn():
N <-- 0
time <-- 0
start:
N++;
if(time>1) break;
update(time);
print N
#include <stdio.h>
#include <time.h>
#include <math.h>
void big_o_n();
int main()
{
//function to calculate the value of n using O(n) time
big_o_n();
return 0;
}
void big_o_n()
{
clock_t start, end;
unsigned int n = 0;
double sec = 0;
start = clock();
while (sec < 1)
{
n++;
end = clock();
sec = (double)(end - start) / CLOCKS_PER_SEC;
}
printf("Value of n with O(n) complexity in 1 second:: %u\n", n);
}
Value of n with O(n) complexity in 1 second:: 1540555
n
and lets discuss for O(N^2)
.O(N^2)
time complexity when we actually don't know the value of N
, Instead we have to calculate the value of N
.N
Natural numbers is N(N+1)/2
. So we can use this property to perform this experiment.1+2+3....+N = N(N+1)/2 <-----Eq(1)
i
times where i
incremented by 1
for every outer loop iteration. So for more clarity let see the algorithm.void bigOnSquare():
time <-- 0
n <-- 0
i <-- 1
sts <--false
while(i):
n <-- n+1
for j <-- 0 to i:
update(time)
if time>=1:
sts <-- true
break
if sts:
break
i<-- i+1
print n
i
with 1
and inner loop every time iterate i
times. we are incrementing value of i
by 1
in every outer loop iteration. This means that inner loop will be iterate 1 + 2 + 3. ..... times
for one second equation 1
: 1+2+3..+n = n(n+1)/2
which is nothing but the operation performed for O(n^2)
times.#include <stdio.h>
#include <time.h>
#include <math.h>
void big_o_n_square();
int main()
{
//function to calculate the value of n using O(n^2) time
big_o_n_square();
return 0;
}
void big_o_n_square()
{
clock_t start, end;
unsigned int n = 0;
double sec = 0;
int sts = 0;
start = clock();
for (int i = 0;; i++)
{
n++;
for (int j = 0; j < i; j++)
{
end = clock();
sec = (double)(end - start) / CLOCKS_PER_SEC;
if (sec >= 1)
{
sts = 1;
break;
}
}
if (sts == 1)
{
break;
}
}
printf("Value of n with O(n^2) complexity in 1 second:: %u\n", n);
}
Value of n with O(n^2) complexity in 1 second:: 1774
n
and lets discuss for O(N^3)
.O(N^2)
experiment it is also based on a well defined formula. But what is it? Think and then go ahead.10th
standard which is nothing but the sum of squares of first n
natural numbers.1^2+2^2+….+n^2= n(n+1)(2n+1)/6 <------------Eq(2)
void bigOnCube():
time <-- 0
n <-- 0
i <-- 1
sts <--false
while(i):
n <-- n+1
for k <--0 to i:
for j <-- 0 to i:
update(time)
if time>=1:
sts <-- true
break
if sts:
break
if sts:
break
i<-- i+1
print n
#include <stdio.h>
#include <time.h>
#include <math.h>
void big_o_n_cube();
int main()
{
//function to calculate the value of n using O(n^3) time
big_o_n_cube();
return 0;
}
void big_o_n_cube()
{
clock_t start, end;
unsigned int n = 0;
double sec = 0;
int sts = 0;
start = clock();
for (int i = 0;; i++)
{
n++;
for (int j = 0; j < i; j++)
{
for (int k = 0; k < i; k++)
{
end = clock();
sec = (double)(end - start) / CLOCKS_PER_SEC;
if (sec >= 1)
{
sts = 1;
break;
}
}
if (sts == 1)
{
break;
}
}
if (sts == 1)
{
break;
}
}
printf("Value of n with O(n^3) complexity in 1 second:: %u\n", n);
}
Value of n with O(n^3) complexity in 1 second:: 168
n
and lets discuss for O(N^3)
.n
power of 2
is equal to 2^(n+1) -1
. which is nothing but the O(2^n)
which is also called as exponential
time complexity. If you want you can google this algorithm and find the explanation. But for now lets first see the equation and then implement it.2^0 + 2^1 + 2^2 + 2^3 + …. + 2^n-1 = 2^n - 1 <<----------Eq(3)
void bigOnSquare():
time <-- 0
n <-- 0
i <-- 1
sts <--false
while(i):
n <-- n+1
for j <-- 0 to pow(2,i):
update(time)
if time>=1:
sts <-- true
break
if sts:
break
i<-- i+1
print n
#include <stdio.h>
#include <time.h>
#include <math.h>
void exponential();
int main()
{
//function to calculate the value of n using O(2^n) time
exponential();
return 0;
}
void exponential()
{
clock_t start, end;
unsigned int n = 0;
double sec = 0;
int sts = 0;
start = clock();
for (int i = 0;; i++)
{
n++;
for (int j = 0; j < pow(2, i); j++)
{
end = clock();
sec = (double)(end - start) / CLOCKS_PER_SEC;
if (sec >= 1)
{
sts = 1;
break;
}
}
if (sts == 1)
{
break;
}
}
printf("Value of n with O(2^n) complexity in 1 second:: %u\n", n);
}
Value of n with O(2^n) complexity in 1 second:: 21
Value of n with O(n) complexity in 1 second:: 1540555
Value of n with O(n^2) complexity in 1 second:: 1774
Value of n with O(n^3) complexity in 1 second:: 168
Value of n with O(2^n) complexity in 1 second:: 21
34