Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big O N^2 (Log N)

I am a complete newbie at Big O and I am a bit stumped by this. I have:

for (int i = 1; i < n*n; i *= 2)

In my mind this would equate to N^2 * Log N

Am I right or can it be simplified to N as you are doubling the inputs with n*n and halving it with i *= 2?

like image 433
Matt Avatar asked Aug 23 '15 14:08

Matt


People also ask

What does big on log n mean?

Logarithmic time complexity log(n): Represented in Big O notation as O(log n), when an algorithm has O(log n) running time, it means that as the input size grows, the number of operations grows very slowly.

What is the big O of 2 n?

O(2n) An example of an O(2n) function is the recursive calculation of Fibonacci numbers. O(2n) denotes an algorithm whose growth doubles with each addition to the input data set. The growth curve of an O(2n) function is exponential - starting off very shallow, then rising meteorically.

What is Big O of n log n?

O(nlogn) is known as loglinear complexity. O(nlogn) implies that logn operations will occur n times. O(nlogn) time is common in recursive sorting algorithms, sorting algorithms using a binary tree sort and most other types of sorts. The above quicksort algorithm runs in O(nlogn) time despite using O(logn) space.

Which is greater n 2 or log n?

So, O(N*log(N)) is far better than O(N^2) . It is much closer to O(N) than to O(N^2) . But your O(N^2) algorithm is faster for N < 100 in real life. There are a lot of reasons why it can be faster.


1 Answers

In this case you have

O(log2(n ^ 2))

which is

O(2 * log2(n))

or just

O(ln N)

note if n * n > (1 << 30) you will have an infinite loop.

like image 111
Peter Lawrey Avatar answered Sep 28 '22 17:09

Peter Lawrey