I need to design an algorithm that is able to do some calculations in given O notation. It has been some time since I last calculated with O notation and I am a bit confused on how to add different O notations together.
O(n) * O(log n) = O(n log n)
O(n) + O(n) = O(2n) = O(n)
O(n) * O(log n) + O(n log n) = O(n log n) + O(n log n) = O(n log n)
Are these correct? What other rules have I overlooked?
The rule for multiplication is really simple:
O(f) * O(g) = O(f * g)
The sum of two O terms is harder to calculate if you want it to work for arbitrary functions.
However, if f ∈ O(g), then f + g ∈ O(g).
Therefore, your calculations are correct, but your original title is not;
O(n) + O(log n) = O(n)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With