Hi, everyone.
So in this video, what we will do, we will write the code for the partition function.
So I told you there are many ways you can write the partition function.
And I discussed one of the approaches with you guys.
So I hope you guys can code that approach yourself.
So I am discussing one more approach.
So let's discuss one more approach for writing the partition function.
So what do we do?
So let's take one example, eight five, two, one, seven and three and this is four.
So four is our world index.
This is the START index.
This is the end index.
So four we call for as our pivot.
So this is called pivot element.
Simple.
So what we will do, you will take two variables.
So you will take very variables, which essentially are the starting position.
You will take variables, which is initially at the starting position.
Simple, then you will I strategy.
So you will move forward.
Basically you will.
I totally agree with you till this point only did elementary so and minus one.
This is right.
So this is and minus one.
So basically, you will write something like this, Jagels due will start from start, sorry, so Jay
starting Elementalist is starting next.
Gibbsville, Tildy, and minus one, not this element of it, not end and C++, so C they are two situations.
There are two possible cases.
Either the element will be greater than four or the element will be less than four.
So if the element is basically greater, then four, then do nothing.
So it is basically a good then for do nothing.
It is good and four.
So if element is good then four do nothing.
So it is good then for doing nothing then just present here.
So five is good then four do nothing.
So now Gibbsville come here.
So now two is basically less than four.
So when you encounter the situation, what do you do.
You will strap G with I.
So you will slap and you will set by engine.
So strap to an eight.
So it will come here to will come here and what you will do.
So you will move your way forward.
So after slapping, you will do a placeless, simple and what is the condition for slapping the element
is less than full and forward is what we would eliminate.
So the next element is basically so present now.
So one is also smaller than four.
So again, you will do the slapping with the eye.
So I is five now, so five will come here and one will come here.
So I will move forward.
So I is now present here and just now presented.
So seven is a good then.
Fourth element is a good then for doing nothing.
So Jay will move forward.
So three is basically less than four.
So you will swap.
So we will set by engy so strap three with it.
So it will come here and three will come here and we will do eight plus plus.
So finally what is it Eddie.
So your G will reach at end and you will stop and your for loop will stop.
So what is already matter is two, then one, then three, then five, then seven, then eight and then
four.
And we're designing.
So just that this index.
So you'll come out of the for loop and basically your eye is present.
So where is I.
So I is present at the next five.
So I use this index element five.
So now what do you need to do the last step after coming out of the loop, after coming out of the loop,
you will step forward.
You will have the pivot element with this element.
So four will come here and five will come in.
So what is your final area, your final days to one, three, then four, then seven, eight and five.
So now you can see four, is that correct?
All the elements less than for all the elements good than for simple.
So at last, what do you need to do one more time so that you will step off?
So this was a five and this was the last element in your friend.
And then we need to return the spirit index.
So it makes for what is next for I, I so I will return I.
So this is the other way.
OK, this is the approach to writing the petition function.
So I think approach one was easy so you can write the code for the approach one I am writing the code
for approach to, so let's write the code.
So we need two variables.
I think I will be at the starting position starting next.
Initially, Jay will also be at the starting next.
So now we will use a follow up to our over the area.
So G so we will use Jane's inside the for loop.
So Jay will start from starting Next G less than articles two and minus one and minus one angioplasties.
And what is our favorite element?
So the last element is called the pivot element.
So our pivot is basically aof and simple.
So if the element is less than the pivot element, we will trap.
So if the current element, which is agea is less than the pivot element, I will do the swapping.
So I will swap High-Energy.
And I will move forward and will discuss that at the end, we need to set one more time.
We need to satisfy and offend and we will return.
So this is the thing that we discussed so you can see.
At the end, we need to step at the end, we are stepping in tonight for the partition and so we are
turning.
And if the element is smaller, we are stepping.
So we are stepping.
So if it is less than pivot, so element is to then for do nothing, but it is less than the pivot element
you need to strap.
You need to step in to do a plus.
Plus I'm doing a plus plus and the pivot element basically the last element.
So the last element is the pivot element.
And I told you where do we go to land minus one.
So we will trade only on this.
So let's test one more time how our function will work.
So the elements let's take the previous one example, so eight five two one seven.
Three and four.
So what I'm doing here I is they're starting next, this is your day, this is your pivot is off.
And so this is a pivot element for discoverability.
It will go to the minus one.
So Jay Z starting position, it will vote till three.
It will go till three.
So element is less than pivot and pivot.
No element is less then pivot.
So pivot is for the survivors less than for No.
So, too, is less than four, if two is less than four.
So I will do the swapping, so strap it will come here to will come here and I plus.
Plus.
OK, C++, now let's check for the next element, so the G will also move forward, we are doing C++,
so G comes here.
So one is less than four.
Again, do the swapping.
So five will come here, I will move forward.
I placeless, this is a and we will move forward plus plus.
So when is good for do nothing?
Seven is ago then for do nothing.
So plus plus.
So basically, elementary is less than four, so three is less than four do the slapping part, so three
will come here and it will come here.
So I plus plus.
And they are doing C++, so G will come here and it will come out of the followup.
So after coming out of the loop, I'm swiping I and the last element, so I'm stepping five and four,
so so have these two elements.
Four will come here and the five will come here.
So finally, what is my area?
So two, then one, then three, then four, then seven, then eight and then five, and I'm returning,
I so I is present at this.
So this is a which is zero one, two and three.
So I will return the partition and I'm returning the partition and next and you can see so this is all
elements less than four and all the elements were then four, then quicksort on the side.
So this is the starting area.
This is B minus one.
This is B, this is B plus one.
And this is and so I play quicksort on this area.
I play quicksort on this area.
Bodanis will get sorted.
Simple.
So that's how it is working.
So I want to explain one more thing.
That it is not necessary.
It is not necessary that I take the last element as my favourite element.
I can take any element, Desmonte.
Belittlement, so you can also take the starting element as well.
So instead of an element, we do what you can do.
You can select the starting element as it pivot.
And so what will happen, however, for look will change.
So G will start, G will start from start plus one G will go till the last element.
OK, so we can also pick the first element as a pivot element.
It is not necessary to pick the last element as pivot.
OK, so you can choose first element or you can choose the last element as a pivot element.
Simple.
So this is all about quicksort.
This is the working of quicksort.
So this is it from this video.
I will see you in the next one.
Thank you.