A lot of people find Big O scary but one should know it to determine the efficiency of algorithms, so I wrote a thread explaining Big O notation ๐Ÿงต๐Ÿ‘‡
First of all, what is Big O?

It is the way of determining the performance of algorithms as the input grows bigger and bigger.

In other words, how much time and space the algorithm would take to execute as the input grows larger.
Why do we care?

Big O is needed so that we can optimize code so that it would deliver fast performance even when millions of people are using the application at the same time.

Also, it is very important to know about Big O in technical interviews.
Let's talk time complexity:

how the time required to execute an algorithm varies with respect to the size of the input.

Look at the code below ๐Ÿ‘‡ (language: JavaScript)
In the above function, the for loop will run 'n' times, so as the input(n) grows, the algorithm would have to undergo more iterations, and hence taking more time.

so the size of the input(n) is directly proportional to the time taken to run the code.
This is called O(n) time complexity and the graph looks like this:
Now, look at the code below ๐Ÿ‘‡
In the above code, the function takes in an array and prints its first element.

So here this code will take a constant amount of time to run irrespective of the size of the array.

That means there is no relation between input size and execution time.
This is called O(1) type complexity, where the algorithm takes a constant amount of time to run and the graph looks like this:
Let's move on to O(nยฒ), look at the code below:
The function above takes an input(n) and prints all the pairs of numbers that add up and make 'n'.

Here we are using nested loops, so, there are n iterations of the outer loop and for each iteration of the outer loop, there are n iterations of the inner loop.
That makes the total iterations to:

n * n = nยฒ

that is why it is called O(n) type complexity because as n grows the time required to execute the algorithm grows exponentially(raised to the power of 2) because of the growth in iterations.

The graph looks like this:
Now let's talk space complexities:

Space complexity is defined as the extra space required by the algorithm to execute apart from the space required for input.

In other words, how much memory an algorithm takes to execute.
Some things to note before moving:

Most primitive data types(booleans, numbers, undefined, null) roughly takes the same amount of space irrespective of their value.
for example:

x = 10 and x = 100000 would take the same amount of memory.
Same in the case of booleans:

isTall = true
isTall = false

But strings require O(n) space where n is the string length.

for example:

x = '1'
y = '123456789'

here y would take 9 times more space than x because it has 9 characters and x has only one.
Reference types generally require O(n) space where n is the length (for arrays) or the number of keys (for objects).

for example:

x = [1]
y = [1, 2]

here y takes twice as much space as x because its length is 2 times that of x.
Now, look at the code below ๐Ÿ‘‡
The function above takes an array and return the sum of all the items in the array. Now we have to figure out how much space the algorithm takes to execute.

here we have two variables, 'total' and 'i'(in the for loop).
That's all it is, there are only two variables whose space requirement would always be the same no matter the size of the array.

This type of space complexity is called O(1) where the space is constant irrespective of input size. The graph looks same as O(1) time complexity.
Now, look at the code below ๐Ÿ‘‡
The function above takes an array as an input and return a new array with all the elements double of the elements in the input array.

we have two variables here 'i' (in for loop) and 'newArr'(the array that is returned)
The space requirement of 'i' is always the same, but that of 'newArr' increases as the length of the input array increases, because newArr would be of the same length as the input array and as the length of arrays increase, so does their size.
So input size is directly proportional to space requirement, this type of space complexity is called O(n) and the graph looks exactly the same as that of O(n) time complexity.
In conclusion,

Execution time and space requirement order of algorithms:

O(nยฒ) > O(n) > O(1)

that means O(nยฒ) algorithms are slowest and becomes more and more space-intensive as the input size grows while O(1) algorithms are fastest and require a constant space to run.
Here is a cheat sheet for you to remember graphs of different types of complexities ๐Ÿ‘‡
RT's would be appreciated @Prathkum @csaba_kissi ๐Ÿ™.

I am writing a thread daily on data structures and algorithms. Retweet the first tweet and follow me for more content like this. It would mean a lot to me.

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Sahil Aujla

Sahil Aujla Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(