Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this algorithm linear and not linearithmic?

int sum = 0; 
for (int n = N; n > 0; n /= 2)
   for (int i = 0; i < n; i++)
      sum++; 

I was pretty sure it grows in nlogn but was told it's linear... Why is it linear and not linearithmic?

like image 868
SamuelN Avatar asked Jan 23 '13 18:01

SamuelN


3 Answers

It is linear. Imagine for a second n is 64. The inner loop runs 64 times, then 32 times, then 16 times, then 8 times, then 4 times, then 2 times, then 1 time. 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127.

So it requires 2n-1 total operations (for a power of 2, but that doesn't change the analysis), assuming the inner loop is not optimized away. That's clearly O(n) -- linear.

If the inner for loop is optimized away (to sum += n;), it's logarithmic.

like image 60
David Schwartz Avatar answered Nov 09 '22 23:11

David Schwartz


The complexity of this algorithm is Θ(N).

The number of operations is

sum{2**k} for k = 0..log2(N)

The sum of this progression is

2*N-1

which is Θ(N).

like image 33
NPE Avatar answered Nov 09 '22 21:11

NPE


Formally, using Sigma Notation can help you to see clearly that the order of growth is linear.

enter image description here

like image 29
Mohamed Ennahdi El Idrissi Avatar answered Nov 09 '22 21:11

Mohamed Ennahdi El Idrissi