Why is this algorithm O (N)?

The following C code is apparently O (N) (according to my practice exam). However, I'm not sure why this is O (N), and not O (something * something).

void doit(int N) {
    while (N) {
        for (int j = 0; j < N; j += 1) {
        }
        N = N / 2;  
    }
}

Anyone want to give me some idea about this problem?

Thanks in advance!

+4
source share
1 answer

Since N + N / 2 + N / 4 + ... = 2N.

+20
source

All Articles