 Hey, welcome to intro to AP computer science a in Java today. I'm going to talk about selection sort So let's get started. Okay selection sort selection sort is a Sorting method that's pretty simple and it's pretty straightforward and it's I think it's a good one for beginners to learn Just because it's it's yeah, it's not too complex theoretically. Okay, so essentially what you're doing Is you're going through your list now in this case it's integers, but it could be Doubles it could be whatever as long as it's sortable With some parameter, but we're gonna use integers to keep it simple So basically we're gonna do go is you're in your head. You're kind of starting here And so let's call it we can call that I don't really want to talk about the coding so much at this point And I just want to talk about you know, how it works So what we got to do is we got to go through it. We got to find the lowest number Okay, so in this case, that's clearly zero and then what we do is we take zero and We swap it with this first value, which of course in programming This is the zero with a value and we just swap those two values So then we have a zero here. We have an eight here So we know now that this is the lowest value Now we do the exact same thing instead now we start here We go through we find the lowest value in that case. It's gonna be a one So because of that then we swap the one and the five Then we do the exact same thing we go here and now coincidentally this is the lowest number So we're gonna end up not really need to do a swap But we still have to check every single number to see if it is the lowest or not Okay, so what we got to keep track of is our starting point on each round So the first time we start at zero second time we start at one two three four five six seven eight All the way to the end and then we need to keep track of the comparisons So we're comparing zero to one and then one zero to two zero to three zero to four zero to five zero to six So you can see here. We're gonna have a nested loop structure So J is always going to be starting at i plus one Okay, and again until we get to the end we find the lowest value So we also need to keep track of where is the lowest value that we found so far So for example when we do our first run through Okay, we assume that the first value in this case the zero of the value is the lowest So our minimum index is going to be zero and that zero comes from i So we're just going to set i equal to or minimum index equal to i Then we're going to compare it. So we already see that five is lower than eight So our minimum index is going to equal whatever j is in this case one Okay, j goes up to Two and we compare the values and as it turns out this is also lower So minimum index is now j which would be zero one two Then we do the same thing and we just keep going through eventually once we run through that process for every Value all the way to the end we will get a sorted list And I got this information from Wikipedia So the link is here and I'll put that hopefully in the description down below if I don't forget So let's go ahead and take a look at some actual coding and go ahead and see how that works So as I said, we need to keep track of a few different things now This case I'm using Java. So I'm gonna go ahead and make an integer for the minimum index And we're gonna Start that just declare it to be zero and we can we're gonna change that in a minute anyway And then in my for loops, I'm gonna go ahead and declare int i Equal zero because we're starting at zero and then we're gonna go to i is less than the array dot length We could actually do minus one here, but let's just try with just the array length. I think it should still work i plus plus Okay, and then in here we're gonna say our min Index Equals i now the first time through it's zero and zero doesn't matter. It's gonna work, but the second time through I is gonna be starting at one So we need to make sure that the minimum index when we start is set up to That value okay of i in this case and Then we need our second loop for int j equals i Plus one if you recall from the explanation i it's i and then j is always i plus one That's gonna keep changing as we run through the loops and then j of course is also have to be less than the array dot length And then j plus plus So in here now we need to see if the element that we're looking at is the lowest element, okay, so we're gonna say if Array j is Less than array min index so if the current number is less than the the Like the value of the minimum current minimum then we need to we need to say okay. Well, that's that's our new minimum Now notice we're not actually keeping track of the value itself We're just keeping track of its location. So then we would say min index equals j All right, and we're gonna let that run through to the end of the array and then what we got to do actually We're just gonna let that do its thing and then if it is Different so basically what I'm gonna do here is if the min index Does not equal i In the case where i is already The minimum value now we don't actually have to do that we could do it. It's it's still gonna work That's a little inefficient So what I'm gonna do is I'm gonna say int Temp Equals See a r r i because we're swapping out i and I'm gonna say a r r i Equals a r r the r Min index and then I'm gonna say a r r min index Equals our temp value. All right, and that should do it. I'm hoping so I'm gonna run that and see what happens Oh my gosh, it worked. So zero one two three four five six seven eight nine So we took this array and we Swerved it and as I mentioned because we're going from you know Because j is i plus one I could I could I think put a minus one here and it should still work Let's try that just to make sure See if we get an error. Okay, and that still does work. Okay, so again Just conceptually what we're doing is we're going through the list Are the the array? Excuse me each time We're trying to find the lowest value and we swap that out with the current position that we're in So in the case I only actually go back to here So we're starting with I and then we're looking for the lowest value. We find it We swap it out Then we start here because we know that's the now the lowest of all the numbers and then we start here And we look for the lowest number and we find it we swap it out Look for the lowest number find it swap it out look for the lowest number find it swap it out Etc. Etc. And once we are done we have a fully sorted list. Okay, that is it. Good luck