Hi, i have been given some code to work out big o runtimes on them, could someone tell me if i am on the right track or not??
//program1
int i, count = 0, n = 20000;
for(i = 0; i < n * n; i++)
{
count++;
}
Is that O(n^2)?
//number2
int i, inner_count = 0, n = 2000000000;
for(i = 0; i < n; i++)
{
inner_count++;
}
is this one O(n)?
//number3
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
count++;
}
}
O(n^2)?
//number4
for(i = 0; i < n; i++)
{
for(j = 0; j < i; j++)
{
for(k = 0; k < j; k++)
{
inner_count++;
}
}
}
Is that O(n^3)?
//number5
int i, j, inner_count = 0, n = 30000;
for(i = 0; i < n; i++)
{
for(j = 0; j < i; j++)
{
inner_count++;
}
}
Is that one O(n^3)?
//number6
int i, j, k, l, pseudo_inner_count = 0, n = 25;
for(i = 0; i < n; i++)
{
for(j = 0; j < i*i; j++)
{
for(k = 0; k < i*j; k++)
{
pseudo_inner_count++;
for(l = 0; l < 10; l++);
}
}
}
Very confused about this one O(n^3)??
//number7
int i, j, k, pseudo_inner_count = 0, n = 16;
for(i = n; i > 1; i /= 2)
{
for(j = 0; j < n; j++)
{
pseudo_inner_count++;
for(k = 0; k < 50000000; k++);
}
}
o(n)? (I get more lost as they get harder)
//number8
int i, j, pseudo_inner_count = 0, n = 1073741824;
for(i = n; i > 1; i /= 2)
{
pseudo_inner_count++;
for(j = 0; j < 50000000; j++);
}
O(n^2)?
one more i didn't see and have absolutely no idea with {
int i, wrong_count = 0, n = 200;
for(i = 0; i < square(n); i++)
{
wrong_count++;
}
printf("%d %d\n", wrong_count, square(wrong_count));
return 0;
}
int square(int m)
{
int i, j = 0;
for(i = 0; i < m * m; i++)
{
j++;
}
return j;
}
If anyone could clarify these and help me understand them better I would be very grateful.
Cheers