Hey, guys, what's up?
So in the last video, we talked about Bubble.
And I told you that the Bulls takes and square a number of steps to sort our Eddie.
OK, so, for example, if my input is, let's say three, four, seven, one, 10.
So if my input is this, how many number of steps it will take, it will take and square a number of
steps.
OK, so if my ETA is one, three, four, seven and a 10, so if my input is this, then also it will
take into a number of steps.
OK, so this area this area is unsorted, Eddie.
This ad is unsorted today, then it is taking any number of steps.
This area is already sorted, this area is already sorted, then also it is taking and a number of steps
to sort this area, which is already certain.
OK, so if I give this as input, maybe it will take and number of steps if I give this as in, but
then also bolster this digging into a number of steps.
So our aim is to optimise.
Our aim is to optimize bubbles out.
So that if the input is unsorted, it can take any number of steps, but if the error is already sorted,
it will take and steps.
OK, so we want to achieve this.
So what we want we want to optimize our budget so that if the given it is already sorted, then it should
take any steps.
So how we can optimize our budget.
So let's take an example.
Let us try to understand with the help of an example, so suppose the given area is two, four, seven,
eight, nine and 12.
So the given area is already sorted.
OK, this given that is already sorted, let us implement bubbles out on it.
So.
Compared no slapping.
No sweat compared, no sweat.
Compare notes, web.
Compare notes app.
Compare Nosef.
That means if the given area is already sorted, then no slapping will take place.
OK, so I will not be there.
Slapping will not be there.
OK, so we can use this property.
OK, we will use it, we will use it that if the given array is already sorted, if the given that is
only distorted, then something will not be there.
We will use this so that our sorted so that if the input is already sorted, then bubble start, take
and steps.
OK, so how we will do it, so let's see.
So let's nameless, faceless, optimized bubble.
Optimized bubble bubble are large c.p.
So what I will do, so this was our old heart, so we will cooperate.
And Bastad here.
So what optimization we have to do?
So this swab thing, we have to optimize this, so what I will do?
Let us take a variable and flag.
So I'm taking a variable and flag, which is initially zero.
OK, so I am taking our flag variable, which is initially zero.
Now I will enter this loop.
OK, so in this loop, so if there is swiping what I will do, I will change the value of flag to one.
OK, so that means if the intruder is unsorted, this condition will be true at least once, this condition
will be true and and these two men should be executed.
So when this for Loop and I will check the value of flag.
So if flag is still zero, if flag is still zero, that means the is already started.
So what you can do is break.
So these three lines, initially my flag also zero.
So if that is sorted, this condition will always be false.
OK, if that is sorted, this condition will always be false, hence these two lines will never be executed
and this fall will terminate, then I'm checking if the value of flag is still zero.
That means you can right here.
That means my error is that it is already sorted.
So what you should do is come out of the loop.
OK, so come out.
Come out of the loop.
OK, so what this extraordinary do so this public statement will break this outlook.
OK, so this was closing and this was happening.
So this big statement will break this loop.
I will come out of this loop and this is where my function and OK, so this is all that we have to do.
Now, this is called optimized mobile art.
OK, it will take any steps, vital taken steps.
So my flag is zero, so let's say that elements are one, two and three, so I will check JMJ Listen,
compare no slapping compar, no slapping.
So I will reach this lame flag.
Flaggers still zero.
So flaggers still zero break.
Come out of this loop.
So how many number of steps it took.
As many number of times as this loop is running, so this loop is running and times.
OK, it is actually raining and minus two times or minus one times, but it doesn't matter.
OK, this constant one, two, three, there doesn't matter.
We always report our answer in terms of.
And so the slope is ending and times.
So this optimized budget will take and steps if that is already sorted out.
OK, so the main logic was just to use a variable flag.
If there's a spring that means that is unsorted.
That means there is unsorted and the value of flag will change, the value of flag will become one.
And this condition will become false.
So Blake will not be executed.
OK, so this was the whole logic.
OK, so if you have any doubt regarding the optimized verbal assault or regarding the Bolstad, you
can ask me, OK, feel free to ask.
So this is all for this video.
Thank you.