Question

Will performing a O(log N) algorithm N times give O(N log(N))? Or is it O(N)?

e.g. Inserting N elements into a self-balancing tree.

int i = 0;
while (i++ < N) {
    insert(itemsToInsert[i]);
}
Was it helpful?

Solution

It's definitely O(N log(N)). It COULD also be O(N), if you could show that the sequence of calls, as a total, grows slow enough (because while SOME calls are O(log N), enough others are fast enough, say O(1), to bring the total down).

Remember: O(f) means the algorithm is no SLOWER than f, but it can be faster (even if just in certain cases).

OTHER TIPS

N times O(log(N)) leads to O(N log(N)).

Big-O notation notates the asymptotic behavior of the algorithm. The cost of each additional step is O(log N); we know that for an O(N) algorithm, the cost of each additional step is O(1) and asymptotically the cost function bound is a straight line.

Therefore O(N) is too low of a bound; O(N log N) seems about right.

Yes and no.

Calculus really helps here. The first iteration is complexity log(1), the second iteration is log(2), &ct until the Nth iteration which is log(N). Rather than thinking of the problem as a multiplication, think of it as an integral...

enter image description here

This happens to come out as O(N log(N)), but that is kind of a coincidence.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top