Big O Notation
To measure the performance of the code, we use something called Big O notation. It is used for determining time and space complexity of the code. We have 4 rules when it comes to determining Big O Notation:
- Always use the worst case: Even if best case results in
O(1)
, if the worst case isO(n)
, the algorithm is still atO(n)
. - Drop the constants: If we have
O(1/2n + 1)
, we drop the constants and turn it intoO(n)
- Drop the non dominant: If we have
O(n! + n^2 + 99n)
, big O isO(n!)
- Different terms for different inputs: If we have 2 different inputs passed as an argument to the function, we use 2 different terms to represent their complexity (ie.
O(m)
andO(n)
). So if we have 2 loops that depends on each input, we would haveO(m + n)
instead ofO(2n)
.