We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4
Big O
A Measure to the worst-case scenario for a
A way of comparing two sets of code mathematically about how efficient they run. We measure time complexity. But it is not measured in time, but in number of operations it takes to complete something.
Rules for simplifications for Big O
1. Drop constants: If Big O = O(n+n) = O(2n) It can be simplified to O(n) 2. Drop non-dominant: If Big O= O(n2)+O(n) = O(n2+n) It can be simplified to O(n2) 3. Different terms for inputs If Big O=O(a) + O(b) it cannot be simplified further as a <> b Examples on how to calculate Big O: 1- If we have one loop, Big O= O(n), n is the max number of operations this loop takes.
2- If we have 2 loops, Big O= O(n)+O(n)=O(2n)=O(n)
3- If we have 2 nested loops, Big O=O(n*n)=O(n2)
4- If we have 2 loops of different lengths (inputs), Big O= O(a)+O(b)
4. Big O= O(1) O (1) does not mean that there will be only one operation; but it means that as n grows, the number of operations stays constant.
O(1) is the most efficient Big O
Big O for array Lists: - If we want to add/remove an element to the end of the list, then no re- indexing needed so Big O = O(1) - If we want to add/remove element from the beginning of the list, so we need to re-index the whole list, Big O= O(n) , n is the arraylist length - If we want to add a new element to in the middle of the arrayList, Big O= O(n-i) = O(n), i is the index at which we will insert the new element and start re-indexing the remaining list. - If we search for an element by index, Big O = O(1) - If we seatch for an element by value, Big O= O(n)