Welcome back.
Let's talk about the very first roll when it comes to big oil, that is worst case when calculating
big oh, we always think about the worst case.
What do I mean by that?
Well, if we go back to Finding Nemo example.
And the Find Nemo function.
You may have been screaming at me while I was going through the function saying that, Andre, this
is not efficient.
What are you doing?
Because if you look at this function, we're looping through the entire array to find Nemo.
Remember how we had the everyone tray with about 10 different characters from Finding Nemo?
Well, Nima was only the one, two, three fourth member on this array, and when we run this function.
We found Nemo, but the funny thing is this function ran 10 times, not four times.
If I do a console log here and I say.
Running and let's click this.
We see that this function ran one, two, three, four, five, six, seven, eight, nine, 10.
Hmm.
I mean, we already found nimo.
All these runs are pretty wasteful.
So we can make this function a little bit more efficient in JavaScript, we can just have something
called break.
And again, different languages have different ways of exiting out of a loop if a condition is met.
In our case, if we find Nemo, just break out of this loop.
If I do this and click run.
Look at that, once we found Nemo, we're done, we're not going to loop through the rest of the items.
Congratulations, we just made our code a little bit more efficient.
That's a good thing, right?
Well, when it comes to Big O, although this is important and when you write good code, this is something
that we want to do in the big scheme of things.
Bigo only cares about the worst case.
What is the worst case here?
Well, the worst case is that nimo, instead of being the fourth item is at the very end.
So even if we have this brake statement, we're still going to run this 10 times because nimo at the
end, best case is if Nemos at the very beginning and we only have to live through it once.
But worst case, we're still going to have to go through 10.
Loops still big o of nd so this is our very first rule, the very first rule is we always care about
what is the worst case scenario, because when we talk about scalability, we can't just assume things
are going well, even though the Find Nemo function might be of one if Nemo.
Is the very first item in the array.
It doesn't matter in the grand scheme of things, because we can't be certain of what the input is going
to be, we're going to assume that of Bigo is of non linear time.
Again, if we're finding a yellow box.
And the yellow box is at the very end, we have to iterate through all the boxes.
If the yellow box at the very beginning, you see that we don't have to iterate through these boxes.
But Bigo doesn't really care, even though this function, yes, is indeed more efficient, at the end
of the day, when we talk about Bigo, we're talking about worst case.
All right, I hope you got that point, too, I feel like I've beaten to a pulp, so you should be comfortable
with it by now.
Let's talk about the next role on the next video.