Reputation: 581
I am preparing for software development interviews, I always faced the problem in distinguishing the difference between O(logn) and O(nLogn). Can anyone explain me with some examples or share some resource with me. I don't have any code to show. I understand O(Logn) but I haven't understood O(nlogn).
Upvotes: 45
Views: 70909
Reputation: 5275
Think of it as O(n*log(n))
, i.e. "doing log(n)
work n
times". For example, searching for an element in a sorted list of length n
is O(log(n))
. Searching for the element in n
different sorted lists, each of length n
is O(n*log(n))
.
Remember that O(n)
is defined relative to some real quantity n. This might be the size of a list, or the number of different elements in a collection. Therefore, every variable that appears inside O(...)
represents something interacting to increase the runtime. O(n*m)
could be written O(n_1 + n_2 + ... + n_m)
and represent the same thing: "doing n
, m
times".
Let's take a concrete example of this, mergesort
. For n
input elements: On the very last iteration of our sort, we have two halves of the input, each half size n/2
, and each half is sorted. All we have to do is merge them together, which takes n
operations. On the next-to-last iteration, we have twice as many pieces (4) each of size n/4
. For each of our two pairs of size n/4
, we merge the pair together, which takes n/2
operations for a pair (one for each element in the pair, just like before), i.e. n
operations for the two pairs.
From here, we can extrapolate that every level of our mergesort takes n
operations to merge. The big-O complexity is therefore n
times the number of levels. On the last level, the size of the chunks we're merging is n/2
. Before that, it's n/4
, before that n/8
, etc. all the way to size 1
. How many times must you divide n
by 2 to get 1
? log(n)
. So we have log(n)
levels. Therefore, our total runtime is O(n (work per level) * log(n) (number of levels))
, n
work log(n)
times.
Upvotes: 85