Hey, guys, what's up?
So our today's topic is starting.
So we're just not meaning or starting.
So, for example, my Eddys.
Let's say it is seven, five, three, six and eight.
So, for example, this is my Eddie.
Now, after sorting this, Eddie will become.
Three, five, six, seven and eight.
Okay, so this is starting.
So this is my unsorted Eddie and this is my sorted Eddie.
OK, so sorting means these elements.
So I have sorted my data.
So the details sorted in ascending order, the elements are in ascending order.
We can also sort that 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 starting, we generally means that increasing order.
OK.
So we generally mean that sorting means increasing order.
So DeLeon's in increasing order.
So this is General.
OK.
So when we say SA Dairy, that means saw 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, Selection's art bubbles, art insertion, counting Bucket
Reddick's and marriage quick.
And there are many more.
OK.
So there are many sorting algorithms that Lusardi Eddie.
So in this we do we will talk about selection, Saad.
OK, so let us try to understand how this election site works and how it will SORDI data.
So selection sort.
So I suppose let us take an example.
So suppose this is my area.
So the elements are, let's say a seven five, three, six ended above example.
OK.
Now word selection is will say take one point ahead.
Let's say named 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 to eBid seven.
OK.
So compared to David seven.
Which one is smaller?
Three is smaller than slap.
Since.
Trees.
Smaller.
What I will do?
I will swap.
So three will come here.
And seven will come here.
Rest.
All the elements will sit.
Will be same.
Five, six and eight.
OK.
So since three is smaller than seven, I will do swapping.
So here, slapping take place.
Now, what will happen this point that I will move ahead now I will point to five.
So we have to find the smallest element in that 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 the thing.
Keep Daddy as it is.
So my area will remain same.
Why?
Because five is less than six.
So three, five, seven, six and eight.
No, my pointer 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.
So my edit 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 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.
Compare it with seven.
So seven is the smallest element.
So do nothing.
Do nothing.
There is no need to do anything.
So no.
It is.
So this is Mary, three, five, six, seven and eight, and my pointer will reach here.
OK, so this winter.
We'll reach here.
This, I point out, is moving in this direction.
Now I will reach hit now, find the smallest element in the right hand side.
So right hand side 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 is altered in ascending order.
OK, so this is how Selection's art works.
My area gets sorted.
Let's take one more example.
OK.
To make things clear.
So.
Let's say my error is.
Four, five, three, one and two.
So these are my added elements.
So what's selection side will see.
But 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 swapping.
So one will come here for will come here.
Rest all will remain same.
So five, three and two.
OK.
Now my this pointer will come 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 some things or two will come here and five will come here, arrest all the elements
will remain same.
So when 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 four.
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 compared five with four since five
is good.
Four.
Don't do anything.
So do nothing.
And my ad is one, two, three, four and five.
Now, what will happen?
This pointer will reach Paire.
And my right hand side doesn't exist.
So stop, stop.
OK.
So this is my final answer.
And you can see our error 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 lady's side called.
So first of all, I need a variable.
So first of all, I need one loop which loop this loop.
This eye is starting from zero and it is moving in this direction.
So I need our outer loop for I equals zero.
I will start from zero and I will go then less than or equal to and minus two and a plus plus.
So this is.
And minus two.
OK.
Then I will become equals to end minus one.
That is when I will reach here.
My right hand side doesn't exist.
OK.
So I am going this way Ed..
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 vote in 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 loop will start from a plus one.
So this loop will start from let's take a variable G.
So this loop will start from a place one way, a plus one, because if I is here, I have to search
on the right hand side.
So I will start from I E-Plus when.
So starting from a place when we have to check the last position so g less than or equal to N minus
one.
We have to go to last position G plus plus.
What do we 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 that ident side.
OK.
So we will be having the smallest element in that identified.
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 swapping.
So when will you come here?
And four will come here otherwise and do nothing.
So this is how it works.
OK, now let us write the code.
So let's name this file as selections art.
Not CBP.
OK, so now what do we do?
First of all, we will take areas input.
So give me the number of elements in the.
And now I will take input so far.
And I was zero.
I less than an A plus plus.
What I will do, I will take inputs or see in a off I.
OK, so first of all, it does make any.
So the size of the arrays.
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 selection 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 area.
So what do we do with that?
Don't drop this election's art so they don't dive will be wide.
Because this function will not return anything.
It will sort the eddy.
Okay.
And from here we will call selection's art.
So selection.
And it will take Eddie and the size of the Eddie as input.
OK, so we know they are biased biodefense.
So in the last class we have seen that ad is at Boston Biodefense.
So if I will do some changes here, that changes will be reflected here.
OK.
Now, after doing Selection's art and let us write the code for printing the Eddi, so forth, and I
equals and de equals zero i.e. less than n I.
I listin in a blessed place c out of I.
OK.
So I think that we have to do is we have to implement this function.
We have to complete dysfunction selection, Saad.
So what we have decided first of all, I need a variable.
I need to start from zero.
So you can see here.
We need a variable, I need to start from zero and it will go to and minus two.
OK.
So starting from zero.
So for.
And I equals zero.
I will go to Bill and minus two second last position.
OK.
I bless, bless.
Now what we have to do, we have to find the smallest element in that right hand side.
OK.
So we already know how to find smallest element.
What we used to do is we take a variable small list, 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 a plus one.
OK.
So why I am starting from a plus one?
Because I have to find the smallest element in that I tend side of a.
So you can see here from a place where until Bill last position again, this, too, is the last position.
So G.
Less than articles two and minus one and minus one is deal last perdition in the Eddie J.
Plus Bliss.
And now how to find smallest element.
We already know we will compare the current element with the smallest element.
If this current element is less than my smallest element, where we used to do was we was updating,
we were updating our smallest value.
So smallest equals f g.
Now, when I reach line number fifteen, when this loop will finish, when I reach plane number fifteen,
I know does my Lesterville you.
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 do we have to do?
We'll have to scrap.
So scrap.
A 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 where that our logic is, correct or not.
So the number of elements out of five and we'll lose a seven, five, three, six and eight.
So this is my output.
And we all know our out for days 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 other gold is giving wrong output.
OK.
So what we will do, we will drive an hour called.
So we will take an example and we will drive an our called.
So the example Edward took was our area was seven, five, three, six and eight.
So this was our eddy.
Now, as elections are, I equals zero.
So my eye is at this point.
I will go till the second last portion.
So I will go I will go from this position to this politician.
Correct, smallest equals in mix.
So initially, my smallest.
Initially, my smallest is and makes this place infinity.
OK?
So we have to find the smallest elemental light inside.
So the veil 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 Jaivin gotin last position.
So, Jay, will you go then head?
OK.
So what I'm doing, I'm comparing the current value, L.G., 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 smallest becomes five.
OK.
Now go above J.
Plus plus.
OK, so J will come here.
Now, again, compare the current value that this tree with five cents trees smaller.
I will update the value of smallest.
So smallest becomes three.
Now, a J plus plus so J will reach here.
So compare the current value with the smallest value.
Get to this condition is this condition is false.
Do nothing.
Now a 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 becomes equals two n.
And this loop.
This loop will break.
Or you should say terminate.
This loop terminated.
Okay.
Now I will reach at this line.
So if.
If I.
So if I was seven.
So if HFA is greater than smallest.
So smallest is three.
So seven is good.
Then three.
Yes.
I am accepting the values.
OK.
So three will come here.
And seven will come here.
OK.
So my new area 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 so with this tree.
OK, so instead this is swiping its value from the smallest value with the smallest value.
OK.
So I think you've got the logic where we are then.
Net mistake.
So the mistake is we are slapping LFA.
We are slapping a fight with the smallest value.
OK.
And what we should have done.
We have swept this value.
Would 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 stored that index store index, for example.
This is this is second index.
So zero one and two.
So this is second index.
So instead of storing the value, stored the index.
Store index.
And finally, instead of slapping these two values, what I will do, I will scrap if I would eat off
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 swab if I.
That is seven with a of smallest.
Smallest is three.
Okay, so then these values will be swabbed, which is correct.
OK, so what I am doing.
So in the desk comment this whole called.
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 a small list 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 a plus when Angie will go to Last Perdition.
So N minus one is the last position.
Angie, bless.
Bless.
So I really 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'm standing, this is the smallest value.
So smallest is containing the index.
OK, so now I'm comparing.
So Elsmore list Elsmore Lesterville give me that value and I'm comparing.
So if A of G is less than Aylsworth list then I will update the value of smallest.
So you can against here the smallest variable is storing that index.
And then this loop is terminated.
So when this loop will break.
What do I have?
I will have the index of the smallest value.
So I just have to scrap it.
Or if I get this current value with the smallest value that I have found out in the right hand side.
Now, this good is correct.
OK, so let us 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 Ed is getting started.
One more time.
So a number of elements out of five and element out.
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 days.
One, two, three and four.
So that is getting started.
OK.
So where does it end our program?
Let us right in our goal to get how it is working.
So let us take small example.
So suppose my array's three, two, one and four.
So this is my area.
OK.
I'm taking a small example.
I equals zero, so I is here.
Now, I will go tell here, I will vary from this to this.
Correct.
So this is zero.
This is one.
This is two.
And this is three.
So these are indexes.
OK.
The smallest equals a so smallest.
It was zero.
OK, so Jay will start from a plus one.
So here is Jay, OK.
Now compare EOG compare to so of Jaimie's a of J equals two with a of smallest.
So aof smallest.
Is A of Gedo equals three.
So I am comparing two with three.
OK, so compare two with three.
So two is a smaller two is smaller.
I will update smallest.
So the smallest now is one.
OK.
So smallest is one now.
Incremented G.
So, Jay, when you come here.
Now, I have to compare the value of one with a of smallest.
So Ale's molester's to compare one with two smallest will get ablated.
So the smallest is no index three.
Okay, Jay will come here.
Compare AFGE with each of the smallest.
So aof three.
So it should be two.
It will be two.
So compare Ayodeji with each of the smallest of two.
So afterwards, toys when so compare one with four.
Do nothing.
Now, this loop will terminate because while this loop will dominate because J equals.
And so J will come here, J becomes sane.
So this law will dominate.
I will reach at line number 31 sarap efi.
So if I was three.
If I was three with a of the smallest, smallest is two and a of two is one.
So asvab three and one.
OK, so new and it will be one, two, three and four.
So this is my new eddy.
Now I will come here.
I bless, bless.
I will come here.
And again, we will repeat all the operations, all the instructions, so it will get sorted.
OK, so the main thing that I want to tell you is we will store that index.
We will not store the value.
So here you can see smallest equals G.
We will not store the value.
We will even.
We will store the index.
OK.
So if I have the index.
I can write a small list to get the value.
So I think this elevate them is clear to you now.
OK.
So what do you have to do?
So this is most OK.
So what do you have to do?
You have to write this code on your own.
And you have to brighten this cold.
OK.
Please, please, please.
This is my request.
Take pen and paper and died in this cold.
First, try to understand why this logic was why this logic was wrong.
And then try to understand when I am storing the indexes, how this code is working.
OK.
So if you have any doubt regarding this elections or deliver them, feel free to ask.
OK, guys.
So this is it for this video.
Thank you.