Welcome back.
We just learned our first notation of an.
Or linear time, and we see here that we have a few others remaining.
So let's talk about the next one.
Another very common big notation that you're going to see what happens if we have a function like this.
A function that says compressed 1st box that receives an array of boxes.
And this function simply has console dialog boxes zero, so that is its logging out just the first item
in the box.
What would you say the big O of this function is?
How many steps or operations does this function take if the boxes increase from zero to maybe 10 to
maybe one hundred to one hundred thousand?
What would happen here, ready for the answer?
Well, this.
Is what we call constant time, it's an O of one.
That is, no matter how many times the boxes increase year or however many boxes we have, we're always
just grabbing the first item in the array.
If we look at this with an example, if we had an array of boxes here and we run it through the function,
that just takes the first item in the array.
Well, the number of operations is one, no matter how big this the number of boxes are.
We're only doing one thing.
So it's a constant time.
If we look at this on a graph, if we have.
Element or one box, we do one operation, if we have three again, we still do just one because we're
just grabbing the first item in the array.
If we have, let's say, five.
Same thing.
Seven, same thing.
And what about nine?
Again, same number of operation.
And this is I don't know if you can see the line, but this is just constant time.
It's not linear time like it was where it increases and increases with the number of operations, the
number of operations just stays flat.
But I have a question here.
What if we do something different?
What if we do something like this?
What if we have a function that says function?
Or log first.
Two boxes.
And this takes an array of boxes and is going to console the log.
The first.
Item in the array.
And it's going to console dialogue, also the second item in the array.
How do we measure the bigo of this function?
Well, let me just comment this out for a second, because we don't need this right now and just create
an array called boxes and this boxes has to say zero one, two, three, four and five.
So five items or six in this case because we include zero.
And if we run this function.
Log first, two boxes and we give it the boxes of Ray.
And we click run here, we have zero and one, so we've logged this one and then this one.
What's the number of operations here?
Well, we have.
Oh, of one that is one operation here and then we have over here.
Oh, of one again.
Each time this function runs to operations.
So this function in total is actually running O of two operations every time, so no matter how big
the boxes get, the number of operations here is going to be, too, if we look at this on a graph.
Instead of having all of one like we have before, we have O of two, and then if we had three operations,
I'll just be out of three.
But overall, it's still a flat line.
And this is something we're going to get into later on.
But when it comes to cost and time, we don't care about the nitty gritty of one or two or three of
even a hundred, we round this down to just simply saying, oh, of one.
That is, we have constant time, it's a flat line in terms of scalability, it doesn't matter how big
our inputs are, we're always going to do the constant amount of time on a function.
And if we look at this on a graph, we see that of one over here is the dark green area.
It's excellent.
We love of one because it's very scalable, right.
It doesn't matter how many elements we have, it's always going to run.
The same predictability when it comes to computing is very, very nice.
And all of one is definitely excellent.
OK, so we've learned about linear time, oh, of N and then Konstantine O of one.
Let's do a bit of a fun exercise to really solidify our knowledge here.