DEV Community

Cover image for Rules for Simplifying Big O Notations
Arya Krishna
Arya Krishna

Posted on

Rules for Simplifying Big O Notations

Big O Notation is the language we use for talking about how long an algorithm takes to run. It calculates how efficient is the way we solved the problem.

Check out the basics of Big O Notation here - Learn to Code - Big O Notation

To help us simplify the Big O Notation expressions we follow a set of rules.

  1. Arithmetic Operations are constant. For example your computer takes almost same time calculating 2+2 and 20,465+2.

  2. Variable assignments are also constant. Like incase of arithmetic operations your computer takes the same amount of time to to assign the value to a variable be it x = 3 or x= 509.

  3. Accessing elements in an array by index, or object by key is constant.

  4. In a loop, the complexity is the length of the loop times the complexity of whatever that happens inside the loop. For instance if we have nested loops, we are going to have n^2 runtime.

Let's look at an example code -

function logAtLeast5(n){
for(var i=1, i<=math.max(5,n), (i++)
console.log(i);
}
}
Enter fullscreen mode Exit fullscreen mode

Here we have a loop and this loop is going to go from one to either five or end, whichever one is larger.
What happens as 'n' grows larger so as and continues to grow and grow and grow towards infinity? The big O of this isO(n)because as 'n' grows the number of operations grows in proportion with 'n'. In a graph we can see a general trajectory for O(n).

Oldest comments (0)