You're not a good programmer unless you know what big O notation is. That's why we'll look not only into what that is, but we'll also add 2 other very useful notations: big Omega and big Theta notations.
99% of programmer will be satisfied with knowing only big O notation. Not us though, we're not content with 99%, we want to be in the top 1% percent of programmers. Ideally top 0.01%.
They're used to know and talk about algorithms complexity. They let you know if you've done a good job or not. And also if others have done a good job or not. That's why if you don't know about big O you're not a good programmer, because you've been programming blindly for all this time. Until now.
Basically, if they say your algorithm "is
There's a problem though. If you have two algorithms in the same class, then you won't be able to compare them, you'll need a more in depth analysis. You know both algorithms are fast, but you don't get to know which is faster.
The other two notations are very similar. Big O tells you the worst case. big Omega tells you the best case. And big Theta tells you every case. You don't need the average case because it's redundant. You can still say that "in the average case it will do at most this work", so you can use big O notation.
Let's start with the most important one.
Using non-rigorous mathematical language, we say that a function
What this is basically saying is "constants don't matter" and also "weaker functions don't matter".
Say you have a function
If you have
If you carefully look at the definition you might realize that a function inside
You can see that we can use this to talk about our algorithms complexity.
Say you want to find the max number in an array, how many cells do you have to look into? Since you have to look at the entire array the algorithm is
What happens if you double the array? Exactly, you have to look at double the number of cells. That's what
Congratulations, you're into the 99% of programmers. You can go analyze your code complexity and talk with them about it very easily. Now, you actually could stop reading this blog post, but again, we're not fine with that, we want to get into the 1% of actually good programmers. To do so we'll add 2 new tools under our belt, big Omega and big Theta notations.
Again, first using math language:
We say that a function
If you look closely it's basically the same definition as big O, except for one thing, the direction of that inequality.
That's because, as I previously said, if big O is used for the worst case, big Omega is used for the best case. Big O basically says "your function is at worst like this other one" and big Omega says "your function is at best like this other one".
And finally we have big theta.
We say that a function
Do you remember the "find the max" example? As I said before, you have to look at every cell of the array to find the max. So yes, the algorithm is
You now have 3 very important tools to be able to analyze and talk about code complexity. You can look at the code's worst and best cases. And if they're the same you also know the average case. You are now in the 1% of programmers that know about these. Go and put these tools into good use.