 # Big 0 Notation

People often get scared by the name BIG O, But trust me after reading this post all the doubts and fears you have in your mind will get cleared. I will be covering the whole history of Big O with you and then explain why it the first topic asked to each developer and why Data Structures are important.

### Objectives:

1. Motivate the need for something like Big O Notation.
2. What is Big O?
3. Simplify Big O.
4. What is time complexity and space complexity.
5. Evaluate the time & space complexity of different algorithms using Big O Notation
6. What is logarithm?

Let’s take a example

Suppose we want to write a function that calculates the sum of all the numbers from 1 upto some number n. we can write it in many ways

Below you can see both functions work the same
Now, Can you guess which one is better?

Better in terms of what? Faster, Readable or Less Memory Intensive.
Let’s focus on Faster first.

Once you run this function you will get a log saying the Elapsed time in seconds about the time taken by the function to perform the operation.

Now ,the problem with time is
1. Different machines will record different times.
2. The same machine will record different times.
As, Any machine will have their own specs i3 machine will record different time and i7 will record different time. Even the same machine will record different time based on it’s running process.
So, to measure fast algorithms this method is USELESS.

What if we count the number of operations inside each function.
let’s take a look

After all this things Big O came into place

### Big O Definition

We say that an algorithm is O(f(n)) if the number of simple operations the computer has to do is eventually less than a constant times f(n), as n increases.

• f(n) could be linear (f(n) = n)
• f(n) could be quadratic (f(n) = n  )
• f(n) could be constant (f(n) = 1)
• f(n) could be something entirely different!

• So far, we have been focusing on Time Complexity. how can we analyze the runtime of an algorithm as the size of the inputs increases.
We can also use Big O notation to analyze space complexity: How much additional memory do we need to allocate in order to run the code in our algorithm?

Sometimes you’ll hear the term auxiliary space complexity to refer to space required by the algorithm, not including space taken up by the the inputs.

Unless otherwise noted, when we talk about space complexity, technically we’ll taking about space complexity.

### Rules of Thumb [Space complexity]

• Most primitives(booleans, numbers, undefined, null) are constant space.
• Strings require O(n) space (where n is the string length).
• Reference types are generally O(n), where n is the length (for arrays), or the number of keys (for objects)

In the example you can see single number is declared so the space taken is O(1).

In the second image we have an array and we return the array as new. Here it can be n numbers of items. That’s why it is O(n) Space.

### What is Logarithm?

The logarithm of a number roughly measures the number of times you can divide that number by 2
before you get a value that’s less than or equal to one.

Who Cares about Logarithm time complexity?

1. Searching Algorithm have logarithmic time complexity.
2. Efficient sorting algorithms involve logarithms
3. Recursion sometimes involves logarithmic space complexity.
Don’t Panic by the word Big(O) it is just A identifier used to show how well an algorithm performs.
We will be looking more in this series of Data Structures.
Conclusion:
To analyse the performance of an algorithm, we use Big O Notation. Big O doesn’t care about precision, only about general trends(linear, quadratic, constants).

Scroll to Top