Hey, guys, what's up?
So our today's topic is sorting.
So what is the meaning of sorting?
So, for example, my Eddys.
Let's say it is seven, five, three, six and eight.
So, for example, this is my area now after sorting, this area will become.
Three, five, six, seven and eight, OK, so this is starting.
So this is my unsorted Eddie.
And this is my sorted Eddie.
OK, so sorting means so are the elements.
So I have sorted my data so that it is sorted in ascending order, the elements are in ascending order.
We can also sort the data in descending order, for example, eight, then seven, then six, then five
and three.
So this data is also sorted, but this data is sorted in descending order.
OK, so the elements are in descending order.
So generally when we say sorting, so when we say sorting, we generally means that increasing order.
OK, so we generally mean that sorting means increasing orders or elements in increasing order.
So this is General.
OK, so when we say solidary, that means so that in increasing order.
OK, so why do we need sorting, we need sorting to make our search searching process faster.
So if I have sorted data, I can search much, much faster.
So this is the main purpose of sorting.
Now, there are many algorithms used for sorting, so we have many sorting algorithms.
So some of them are listed here, for example, selections, art lovers, art and accounting, counting
bucket for some quick and there are many more.
OK, so there are many sorting algorithms that will sort the eddy.
So in this video, we will talk about selection, Saad.
OK, so let us try to understand how this election site works and how it will sort the data.
So the selection, Sa'ad.
So let's suppose let's take an example.
So suppose this is my area.
So the arrangements are, let's say, seven, five, three, six and eight above example.
OK, now what it will say, take one point out here, let's say name it.
I so what it will say, find the smallest element in the right hand side.
So which is the smallest element in the right hand side.
I think three is the smallest element compared three with seven.
OK, so compared to David seven, which one is smaller, three is smaller than slap.
Since is smaller, what I will do, I will swipe so that he will come here and seven will come here
against all the elements, it will be same five, six and eight.
OK, so since three is smaller than seven, I will do swapping, so here swapping take place.
Now, what will happen at this point that I will move ahead now I will point to five, so we have to
find the smallest element in the right hand side.
So which is the smallest element in the right hand side?
I think six is the smallest element.
Now compare six with five.
So five is smaller than six do nothing.
So do nothing.
Keep that as it is.
So my area will remain the same.
Why?
Because five is less than six.
So three, five, seven, six and eight.
Now my point, I will reach here because it is moving in this direction now, find the smallest element
in the right hand side.
So I think six is the smallest element now compare six with seven, so six is less than seven.
Do swapping, do swapping.
So my area will look like this.
Three, five.
It and here I am having sex and here I am having seven, OK, now this pointer will move ahead.
Now at this point I will reach at seven.
So find the smallest element in the right hand side.
So it is the smallest element in the right hand side compared with seven.
So seven is the smallest element.
So seven, is this more or less relevant.
So do nothing.
No, nothing.
There's no need to do anything.
So now it is.
So this is my area three, five, six, seven and eight, and my pointer will reach here.
OK, so this pointer.
Will reach here, this apron that is moving in this direction.
Now I will reach here now find the smallest element in the right hand side.
So right inside doesn't exist right inside and doesn't exist.
And this is my final answer.
So this is my final.
Answer and you can see yourself, this ad is started, this ad started in ascending order.
OK, so this is how Selection's art works, my area gets sorted.
Let us take one more example, OK, to make things clear.
So.
Let's say my Ed is.
Four, five, three, one and two, so these are added elements.
So what selections are twenty one point out here now find the smallest element in the right hand side,
so which is the smallest element?
So one is the smallest element compared forward when one is smaller, do sweeping.
So one will come here for will come here list all will remain the same.
So five, three and two.
OK, now what might this pointer welcome here.
Find the smallest element in the right hand side.
So which is the smallest element.
So I think two is the smallest element.
So compared to with five.
Yes, two is smaller, so do swapping.
So we have to do something.
So two will come here and five will come here, arrest all the elements will remain same.
So one, three and four and this pointer will reach here.
Let's call it a day, so I and I now find the smallest element in the right hand side, so I think the
smallest element is for now compare four and three.
So four is greater.
Do nothing.
So do nothing.
There will be no change in the area, so one, two, three, four and five.
Now, my point, I will reach here.
OK, find the smallest element in the right hand side, which is five compar five with four since five
is greater than four, don't do anything.
So do nothing.
And my age, one, two, three, four and five, now, what will happen this point will reach here.
And my right hand side doesn't exist, so I stop.
Stup.
OK, so this is my final answer, and you can see our ETA is sorted.
OK, so this is so this is the thing that we have to do.
OK.
So let's see how we can do this.
So first, let us out cold, so first of all, I need a variable.
So first of all, I need one loop which loop this loop, this is starting from zero and it is moving
in this direction.
So I need outlook for I equals zero.
I will start from zero and I will go still less than or equal to and minus two and I plus plus.
So this is and minus two and minus two.
OK then I will be coming close to end minus one.
That is when I will reach here.
My right hand side doesn't exist.
OK, so I am going to hold this position.
So is this thing clear.
I will go to this position.
If I will come at this position, my right hand side will not exist.
So there is no need.
So I will hotel and minus two.
OK, so inside this loop, what I have to do, I have to find the smallest element in the right hand
side, OK?
So for finding the smallest element, we need a loop.
Remember, we have solved the question how to find the smallest element.
So we need a loop.
So this look will start from a plus one.
So this loop will start from let's take a variable J.
So this will start from a place one Y plus one, because if I is here, I have to search on the right
hand side.
So I will start from IHI plus one.
So starting from my lesson, we have to check the old lost position so g less than or equal to and minus
one, we have to go to last position G plus plus what we will do, we will find the smallest element.
We will write code for finding the smallest element and then this loop will finish.
When this loop will terminate, what we will have, we will have the smallest element in the right hand
side.
OK, so we will be having the smallest element in the right hand side.
Now I will compare if the smallest element on the right hand side is less than my current element.
So what I will do, I will slap.
I will swab I will swab the smallest element, and I otherwise I will do nothing.
OK, so you can see yourself, for example, in this case.
This was I and the smallest element was this one.
OK, so this was our smallest element and this was our current element.
So here I am checking if the smallest element that is if one if one is less than the current element,
that is four, then do something.
So when will come here and four will come here otherwise and do nothing.
So this is how it works.
OK, now let's write the code.
So let's name this file as selection's art.
Not CBP.
OK, so now what do we do?
First of all, we will take that as input.
So give me the number of elements in the area and now I will take input so far and I was zero.
I last and I placeless.
What I will do, I will take input Potosí in if I OK, so first of all, let us make Eddie so the size
of the ads and so I am making an area of size and then I am taking input.
So in the last class we learn functions.
So what we will do, we will make function.
So let's say the name of the function is selecting the site.
So selection.
Selections art is the name of the function.
OK, so what it will take as input, it will take an editor's input and also it will take the size of
the editor.
So what with the return of the selections, so that time will be void because this function will not
return anything.
It will start the editing.
OK, and from here we will call selection art selection.
And it will take Eddie and the size of the ad as input.
OK, so we know they are biased by reference, so in the last class we have seen that it is our passed
by reference.
So if I will do some changes here, that changes will be reflected here, OK?
Now, after doing selections or letters, write the code for printing that, so forth.
And I equals and I equals zero.
I list and I, I listin and I see out aof I.
OK, so that thing that we have to do is we have to implement this function.
We have to complete this function selection.
So what we have decided, first of all, I need a variable I that will start from zero.
So you can see here.
We need a variable, I will to start from zero and it will go to and minus two.
OK, so starting from zero so far.
And I equals zero.
I will go to Bill and minus two second last position.
OK, I placeless.
Now what we have to do, we have to find the smallest element in the right hand side.
OK, so we already know how to find smallest element.
What we used to do is we take a variable small smallest, which is initially intermix, which is initially
intermix, and now we have to find the smallest element in the right hand side.
So I will start my loop from IHI plus one.
OK, so I am starting from my plus one because I have to find the smallest element in the right hand
side of a.
So you can see here from I went to Bill last position again, this, too, is the last position.
So, Jay, less than or equal to and minus one and minus one is the last position in the Eddie Jay Placeless
and now how to find smallest element.
We already know we will compare the current settlement with the smallest element.
If this current element is less than my smallest element.
What we used to do was we was updating, we were updating our smallest value to the smallest equals
G.
Now, when I reach line number 15, when this loop will finish, when I reach line number 15, I know
the smallest value.
OK, so what I will do now, I will compare.
So if my current value, which is if I so if my current value is greater than the smallest value, then
what we have to do, we will have to swap.
So swap of a and smallest.
So this is all that we have to do.
So you can see here, if the smallest element is less than the current element and then we were swapping.
OK, so this is the so this is our entire logic now let's see whether our logic is correct or not.
So the number of elements out of five and values are seven, five, three, six and eight.
So this is my output.
And we all know our art for this coming out to be wrong.
But why this coming out to be wrong?
We have done everything that we have discussed, then why our output is wrong.
So let's see.
Let's try to understand why our code is giving wrong output.
OK, so what we will do, we will write in our code.
So we will take an example and we will try an our called.
So the example that we took was our area was seven, five, three, six and eight.
So this was our last election.
I thought I equals zero.
So my eye is at this point, I will go to the second last position.
So I will go I will go from this position to this position.
Correct, smallest equals endemics.
So initially, my smallest.
Initially, my smallest is and that is plus infinity.
OK, so we have to find the smallest element of the right hand side so the value of G will start from
a plus one.
So the value of.
So the value of J.
We start from here.
So this is my G.
And they will go till last weekend, so they will go tell here.
OK, so what I am doing, I'm comparing the current value f.j. with the smallest value.
So we are comparing five with infinity.
So obviously five is smaller.
So I will update my small list.
So now the smallest becomes five.
OK, now go above Gabler's plus.
OK, so Jay will come here.
Now, again, compare the current value, that is three with five since three years smaller.
I will update the value of smallest.
So smallest becomes three.
Now, plus plus so J will reach here, so compare the current value with the smallest value, OK, this
condition is this condition is false.
Do nothing now.
J will be incremented.
J will reach here.
Compare the current value with the smallest value.
So this condition is again false.
J will reach here.
J will become sequel's to end.
And this loop.
This loop will break.
Or you should say terminate this loop terminated.
OK, now I will reach at this line.
So if, if I so if I was seven.
So if Elfy is greater than smallest.
So smallest is three.
So seven is greater than three.
Yes.
I am accepting the values.
OK, so three will come here and seven will come here.
OK.
So when you add it becomes three five three six eight and smallest is seven.
Now this is the main problem.
You can observe the seven, this is seven should be slapped with this three.
OK, so instead this is swiping its value from the smallest value with the smallest value.
OK, so I think you got the logic where we have the domestic.
So the mistake is we are sweeping Alfy, we are stepping elfy with the smallest value, OK?
And what we should have done, we have swept this value with this value.
OK, so you can see here three appears, two times here should be seven.
So how we can correct this mistake, so the logic is very simple.
Instead of storing the value, so here I am storing the value three, what I will do instead of storing
the value store, that index store index, for example, this is this is an index.
So zero one and two.
So this is second index.
So instead of storing the value, stored the index.
Stoate index, and finally, instead of slapping these two values, what I will do, I will strap HFA
with a ofthe smallest.
So my smallest variable will contain the smallest variable will store the index having smallest value,
so index having smallest value.
OK, so the index of three is two, and finally what I will do, I will set afire, that is seven with
a small child molesters.
Three.
OK, so then these values will be swabbed, which is correct.
OK, so what I am doing.
So let us come this whole court.
And now we will write the characterful.
So there will be some minor changes, so I will start from zero, no doubt I will go till second last
position, so and minus two I plus plus so I am creating a variable smallest.
So this smallest variable will contain the index of the smallest value.
So let's say the smallest value is present at index eye.
OK, so I am saying where I am standing, this is the smallest value, so smallest is containing the
index.
Now I have to find the smallest value in the right hand side, so four and J equals eight plus one,
we will start from eight plus one and G will go to last position.
So N minus one is the last position and G plus plus.
So I will compare.
If the current value, if the current value is less than my smallest value, then what I will do then
smallest will store the index of the smallest value which is Gaono.
OK, so initially I was saying that where I am standing this is the smallest value, so smallest is
containing the index.
OK, so now I am comparing.
So your smallest, smallest will give me that value and I, I'm comparing.
So if F.J. is less than the smallest then I will update the value of smallest.
So you can again see here the smallest variable is storing the index.
And then this loop is terminated.
So when this loop will break, what will I have?
I will have the index of the smallest value.
So I just have to swipe it off.
That is current value with the smallest value that I have found out in the right hand side.
Now, this code is correct.
OK, so let's run our program.
So there are five elements and the elements are seven, five, three, six and eight, so output is
correct.
So you can see my output is correct.
My editors and getting started.
One more time, so no influence out of five and diluent to add one, two, five, four and three.
So my output is one, two, three, four, five last time.
So the number of elements are four and the elements are four, three, two and one, some are today's
one, two, three and four.
So it is getting started.
OK, so that astronaut program, let us bring our goal to get how it is working.
So let us take a small example.
So suppose my array's three, two, one and four.
So this is my area.
I am taking small example.
I equals zero, so I is here.
Now, I will go tell her I will vary from this to this.
Correct.
So this is zero, this is one, this is two and the history, so these are indexes, OK, so smallest.
Accorsi so smallest.
Equals zero.
OK, so J will start from a plus one.
So here is G.
OK.
Now compare F.J. compared to so f.j. means aof G equals two with a smallest so aof smallest that is
of zero equals three.
So I am comparing two with three.
OK, so compared to with three.
So two is a smaller smaller.
I will update smallest.
So smallest now is one.
OK, so it's more or less this one now.
Incrementally, so they will come here.
Now I have to compare the value of one with AOF smallest, so smallest is to compare one with the smallest
will get updated, so smallest is now indexed three.
OK, they will come here.
Compare AFGE with aof smallest, so aof three.
OK, so it should be two, it will be two, so compare AOF with each of the smallest of two.
So afterwards when you compare one with four, do nothing.
Now this will terminate because while this rule will terminate because Jaquiss and so G will come here,
they become cell.
So this law will terminate.
I will reject line number 31 Sep AOF.
So if I was three.
If I was three with a smallest, smallest is two and afterwards one, so ASVAB three and one, OK,
so new and it will be one, two, three and four.
So this is my new editor now, I will come here, I placeless, I will come here and again we will repeat
all the operations, all the instructions.
Solidarité will get sorted.
OK, so the main thing that I want to tell you is we will store the index, we will not store the value.
So here you can see it's more or less because, A.J., we will not store the value.
We will even we will store the index.
OK, so if I have the next.
I can write Elsmore list to get the value.
So I think this algorithm is clear to you now.
OK, so what do you have to do?
So this is most OK.
So what you have to do, you have to write this code on your own and you have to bring this code.
OK, please, please, please.
This is my request.
Take my paper and Barondess code first.
Try to understand why this logic was why this logic was wrong.
And then try to understand when I am studying the indexes, how this code is working.
OK, so if you have any doubt regarding the selections or deliver them, feel free to ask.
OK, guys.
So this is it for this video.
Thank you.