The following C code is apparently O(N) (according to my practice exam). However, I am not sure as to why it 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 care to give me some insight to this problem?
Thanks in advance!
Because N + N/2 + N/4 + ... = 2N.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With