✨ Space and Time Complexity ✨

One of the most important concepts when studying DSA is the complexity of the algorithm

In this thread I will dive deep into what that means and how you can measure it 👇🏻

When learning algorithms you can measure how efficient the algorithm is by knowing how many lines of code it executes to run an algorithm for an input of size n.

However, it is very hard to measure exactly how many lines of code the algorithm takes.

This is why big O exists.
what is big O? 🤔

Big O is the most used asymptotic notation. Asymptotic notations are a way to approximate how many lines of code the algorithm executes relative to the input size which we will refer to as "n" for the rest of the thread
To understand Big O better I will explain it through an algorithm: Selection Sort

This algorithm sorts an array by looping through the whole array and for each element in the array it iterates through the rest of the array to find the smallest element to put it in the beginning
This image visualizes how selection sort works:
in this algorithm we consider the list as the input and n is the size of that list aka the number of elements inside it.

Assume that the if condition takes constant time to be executed.

To know the time complexity we must know how many times the if statement is executed
let's study the inner loop

first when i = 1, the inner loop is executed n times, when i is incremented the inner loop is exectured n-1 times... and so on

if we add them together we will find that the inner loop is executed:

n + n-1 + .... + 2 + 1 times
This is a geometric series!

if we calculate it we will get: n(n-1)/2 which is equal to n²/2 - n/2
when we calculate big O notations we only care about the dominant terms and we do not care about the coefficients

in that case we will remove the coefficients so we will get: n² - n and since n² > n (n² is more dominant), we take n² as our big O notation
therefore, the time complexity of Selection Sort is O(n²)!
Here is a video that explains time complexity in a more detailed way made by @hackerrank
https://t.co/cH0CQjJCqX
🤔 How about space complexity?

Space Complexity is the amount of memory used by the algorithm

if the algorithm needs constant extra space (i.e the space taken is not affected by n) to perform the algorithm its space complexity is O(1)
in our selection sort example we did the sort in place (i.e the original array is changed to a sorted one) so the space complexity is O(1)

what if we want to keep the original array and create a new one to store the sorted values?
In this case the space complexity will be O(n).
🪐 Big O is not the only asymptotic notation out there but it is the most used one.

Want to check out more asymptotic notations? Check out this article from @geeksforgeeks
https://t.co/JxfHb7q5Cw
That's it! if you want to ask any questions my dms are open!

Did you like this thread?
I would appreciate it if you:
- retweet the first tweet
- follow me

I post threads like these and other programming related content :D

More from All

#ஆதித்தியஹ்ருதயம் ஸ்தோத்திரம்
இது சூரிய குலத்தில் உதித்த இராமபிரானுக்கு தமிழ் முனிவர் அகத்தியர் உபதேசித்ததாக வால்மீகி இராமாயணத்தில் வருகிறது. ஆதித்ய ஹ்ருதயத்தைத் தினமும் ஓதினால் பெரும் பயன் பெறலாம் என மகான்களும் ஞானிகளும் காலம் காலமாகக் கூறி வருகின்றனர். ராம-ராவண யுத்தத்தை


தேவர்களுடன் சேர்ந்து பார்க்க வந்திருந்த அகத்தியர், அப்போது போரினால் களைத்து, கவலையுடன் காணப்பட்ட ராமபிரானை அணுகி, மனிதர்களிலேயே சிறந்தவனான ராமா போரில் எந்த மந்திரத்தைப் பாராயணம் செய்தால் எல்லா பகைவர்களையும் வெல்ல முடியுமோ அந்த ரகசிய மந்திரத்தை, வேதத்தில் சொல்லப்பட்டுள்ளதை உனக்கு

நான் உபதேசிக்கிறேன், கேள் என்று கூறி உபதேசித்தார். முதல் இரு சுலோகங்கள் சூழ்நிலையை விவரிக்கின்றன. மூன்றாவது சுலோகம் அகத்தியர் இராமபிரானை விளித்துக் கூறுவதாக அமைந்திருக்கிறது. நான்காவது சுலோகம் முதல் முப்பதாம் சுலோகம் வரை ஆதித்ய ஹ்ருதயம் என்னும் நூல். முப்பத்தி ஒன்றாம் சுலோகம்

இந்தத் துதியால் மகிழ்ந்த சூரியன் இராமனை வாழ்த்துவதைக் கூறுவதாக அமைந்திருக்கிறது.
ஐந்தாவது ஸ்லோகம்:
ஸர்வ மங்கள் மாங்கல்யம் ஸர்வ பாப ப்ரநாசனம்
சிந்தா சோக ப்ரசமனம் ஆயுர் வர்த்தனம் உத்தமம்
பொருள்: இந்த அதித்ய ஹ்ருதயம் என்ற துதி மங்களங்களில் சிறந்தது, பாவங்களையும் கவலைகளையும்


குழப்பங்களையும் நீக்குவது, வாழ்நாளை நீட்டிப்பது, மிகவும் சிறந்தது. இதயத்தில் வசிக்கும் பகவானுடைய அனுக்ரகத்தை அளிப்பதாகும்.
முழு ஸ்லோக லிங்க் பொருளுடன் இங்கே உள்ளது
https://t.co/Q3qm1TfPmk
சூரியன் உலக இயக்கத்திற்கு மிக முக்கியமானவர். சூரிய சக்தியால்தான் ஜீவராசிகள், பயிர்கள்

You May Also Like

1/ Some initial thoughts on personal moats:

Like company moats, your personal moat should be a competitive advantage that is not only durable—it should also compound over time.

Characteristics of a personal moat below:


2/ Like a company moat, you want to build career capital while you sleep.

As Andrew Chen noted:


3/ You don’t want to build a competitive advantage that is fleeting or that will get commoditized

Things that might get commoditized over time (some longer than


4/ Before the arrival of recorded music, what used to be scarce was the actual music itself — required an in-person artist.

After recorded music, the music itself became abundant and what became scarce was curation, distribution, and self space.

5/ Similarly, in careers, what used to be (more) scarce were things like ideas, money, and exclusive relationships.

In the internet economy, what has become scarce are things like specific knowledge, rare & valuable skills, and great reputations.
1/ Some initial thoughts on personal moats:

Like company moats, your personal moat should be a competitive advantage that is not only durable—it should also compound over time.

Characteristics of a personal moat below:


2/ Like a company moat, you want to build career capital while you sleep.

As Andrew Chen noted:


3/ You don’t want to build a competitive advantage that is fleeting or that will get commoditized

Things that might get commoditized over time (some longer than


4/ Before the arrival of recorded music, what used to be scarce was the actual music itself — required an in-person artist.

After recorded music, the music itself became abundant and what became scarce was curation, distribution, and self space.

5/ Similarly, in careers, what used to be (more) scarce were things like ideas, money, and exclusive relationships.

In the internet economy, what has become scarce are things like specific knowledge, rare & valuable skills, and great reputations.