Hi,
This is running time question. I have got following code snippet:
Q. What is the running time of above code?
I have tried to find the running time of each loop separately:
<for(int i=n/2; i<=n; i++)>
if n=4; then i=2, 3, 4
n=8; then i=4, 5, 6, 7, 8
slighly greater than half so running time is log n.
Now for:
<for(int j=1; j+n/2<=n; j=j++)>
if n=4: then j=1, 2,
n=8; then j=1, 2, 3, 4
Again log n.
And for:
for(int k=1; k<=n; k=k*2)
running time is also log n.
There running time = log n * log n * log n
=O(log(n)^3)
Is this correct. Some body please guide me.
Zulfi.
This is running time question. I have got following code snippet:
Code:
int count =0;
for(int i=n/2; i<=n; i++)
for(int j=1; j+n/2<=n; j=j++)
for(int k=1; k<=n; k=k*2)
count++;
}
I have tried to find the running time of each loop separately:
<for(int i=n/2; i<=n; i++)>
if n=4; then i=2, 3, 4
n=8; then i=4, 5, 6, 7, 8
slighly greater than half so running time is log n.
Now for:
<for(int j=1; j+n/2<=n; j=j++)>
if n=4: then j=1, 2,
n=8; then j=1, 2, 3, 4
Again log n.
And for:
for(int k=1; k<=n; k=k*2)
running time is also log n.
There running time = log n * log n * log n
=O(log(n)^3)
Is this correct. Some body please guide me.
Zulfi.