I had to determinate big O complexity of this piece of code. I thought the answer is nlogn but apparently its n. Can anyone help explain why that is so?
void funct(int n)
{
for (int i = n; i > 0; i /= 2)
for(int j = 0; j < i; j++)
printf("%d\n", j%2);
}
That's geometric progression The first time the inner loop is executed n times. The second time it is executed n/2 times. etc... So we have the sequence: n + n/2 + n/4 + ... + 1 so the final formula is:
which is equivalent to n