Big O Notation
Last updated
Last updated
Big O notation is a mathematical notation for describing the complexity of an algorithm. Time and space complexity are two things we care about when it comes to designing algorithms.
is the best case complexity of an algorithm. is the average case complexity of an algorithm. is the worst case complexity of an algorithm.
The time complexity of the following code is .
The constant can be dropped from the expression. For example, can be rewritten as .
Please refer to this cheat sheet for more details.
The time complexity of the following code is .
The following code runs for time.
(and so on) can be simplified to .
The time complexity of the following code is .
We can drop the non-dominant term n, therefore the time complexity is .
means the time complexity is constant, i.e., no matter how large the input size is, the time complexity is always the same.
Binary search is an example of an algorithm that has time complexity of .
The above function has two parameters, a and b, and the time complexity is . It can not be simplified to or .
: my_list.append(item), my_list.pop(), my_list.lookup(index)
: my_list.pop(n), my_list.insert(n, item), my_list.remove(item), my_list.lookup(item) (because the list needs to be reindexed)