Using the below code as an example:
public void method bigO(int N, int M){
PriorityQueue<Integer>> minHeap = new PriorityQueue<Integer>();
for (int i = 0; i < M; i++) {
minHeap.add(i);
}
for (int i = 0; i < N; i++) {
minHeap.add(i);
}
}
The first loop would have time complexity of O(M log(L)) where L is the size/length of the heap. Similarly, the second loop would have complexity O(N log(L)). Since both M and N are linear terms, how would you determine the overall complexity? Would the overall complexity be something like Max(M log(L), N log(L))?
You could think of it like this: since your code is performing both loops sequentially in their entirety, you are doing
Maddcalls and then anotherNaddcalls. That is a total ofM + Naddcalls.Each
addcall has a time complexity ofO(log(L)). But since you are addingMthings in the first loop andNthings in the second loop yourLis growing linearly asM + Ndoes.Putting that together you get
(M + N) * log(M + N)orL * log(L), whereLis the amount of total items you have to deal with.So your code has
O(n * log n)time complexity - linearithmic.