Understanding Time and space complexities in simple terms.
When learning about data structures and algorithms, we encounter a common term: complexities. We see this term everywhere on LeetCode and similar platforms. The efficiency of your solution to a problem is evaluated based on its complexities. In other words, the better your complexity, the better your solution. But this raises the question: what exactly do these terms mean?
What is Time Complexity?
There is a common misconception among beginners that time complexity refers to the total time taken to execute a program. However, this is not the case; the total execution time depends on the configuration of your device. The same code that takes a minute to run on an older device may execute in seconds on a newer device.
Complexity is the computational complexity that describes the time taken to execute an algorithm.
There are different kind of complexities let’s see some of them with examples
O(1) - accessing an element in an array
O(n) - Looping through in an array
O(log n) - Binary Search
O(n²) - Nested Loop
Time Complexity essentially tells us how the runtime of an algorithm grows as we increase the input size.
Let’s take an example to understand this better. Imagine there is a classroom with 10 students. You have given your pen to one of them, but you forgot who it was.
If you ask the first student about the pen and also inquire which of the rest has it, it will take O(n²) time. However, if you go and ask each student one by one, it will take O(n) time.
What is Space Complexity?
In space Complexity as well there is a beleif that it is the space taken by the program during its execution but what exactly is it?
Space Complexity is the amount of memory needs as the input size grows.
While time complexity tells us how fast algorithms run , space complexity tells us
memory how much it uses.lets see some example
O(1) - Sorting algorithms which don’t require extra space.
O(n) - Sorting an array using recursion.
Why do we care?
This is the question that might be on our minds: why do we really care about complexity? Why write complex algorithms when we can solve problems simply?
Let’s consider another example.
Suppose you are using a social media application with millions of tons of data, and the person who coded the app doesn’t care about complexity. What will happen while scrolling? You will experience lagging on the screen. Do you want that? Nobody wants to see a loading circle on the screen; that’s why we need to care about complexity.
Conclusion
Time complexity helps us evaluate how fast an algorithm runs as the input size increases, while space complexity shows how much memory is used as our input grows.
Understanding time and space complexities is crucial in designing efficient algorithms. With this basic understanding, we are ready to dive deeper into optimizing our code.
So, next time you write an algorithm, take a moment to optimize it, and you will be surprised at how small changes can improve your application’s performance.
Subscribe to my newsletter
Read articles from Prerit Saini directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by