‫So now we're going to write our code for selection sort and we'll pick up where we left off in the last
‫video.
‫Except I'll shrink this down into the corner to make room for our code.
‫So we'll pass this an array of integers, and then we'll also pass this the size of the array.
‫And we'll start out with this for loop that iterates through the length of the array where AI is equal
‫to zero.
‫So I'm going to bring this up.
‫AI is going to be this index of zero.
‫And in the last video we started out by setting min index to be equal to zero.
‫And we're going to do that by setting it to be equal to AI.
‫So I'm going to shrink this down and min index is going to be an integer and we'll set that to be equal
‫to I from the for loop.
‫And now we'll create that second for loop and notice that we're setting J to be equal to AI plus one.
‫And I'll bring this back up.
‫We're starting out with AI being equal to zero and J is equal to AI plus one.
‫So we started out with min index being equal to AI, which in this case is zero.
‫But we want to change that if we come across a value that's less than the value that stored at min index
‫and since two is less than four, obviously that is the case here.
‫So we'll set min index to be equal to J.
‫So let's shrink this down and bring back our code.
‫So we'll say if the value at the index of J is less than the value at the index of min index, then
‫we'll set min index to be equal to J.
‫And now let's bring this back up and J is equal to one.
‫So now men index is the index of one.
‫And now that second for loop is going to move Jay through the end of the array.
‫So when we move to the next item, six is not less than two.
‫So we keep looping until we get to here and now.
‫The item at the index of J is less than the item at the Index of Men Index.
‫So we set men index to be equal to J, which is the index of four.
‫And then we continue looping to the end of the array.
‫So now we know the index that contains the minimum value.
‫It's that the index of four.
‫So now we'll swap these.
‫And when we do this, we know that that first item is sorted.
‫So I'll colour that in green.
‫So let's shrink this down and we'll bring in the code that we use to swap those two items.
‫It's going to be these three lines.
‫This is very similar to what we did to swap two items in an array and bubble sort.
‫So I'm not going to walk through these three lines of code because we covered this in bubble sort.
‫So I'm going to bring this up and walk through this one more time.
‫When we go through the four loop again, I is moved to that second item in the array and J is going
‫to be I plus one.
‫So we're going to set men index to be equal to AI, which in this case is the index of one.
‫But you can just look at this and see as we move Jay through the array, there are no items that are
‫less than two.
‫So we're going to move this all the way through and we're not going to change men index.
‫So if men index is equal to the same index that I is equal to, that means we don't need to do a swap.
‫Now, that does mean that the item at the index of one is sorted, so I'll go ahead and colour that
‫in green.
‫But our three lines of code that we use for swapping items does not need to be run here.
‫So we'll create an if statement that says, if I is not equal to men index, only then do we run these
‫three lines of code.
‫So now let's add this into the rest of our code, and that is all of the selection sort function.
‫If we keep running through this, we'll end up with an array that looks like this.
‫So we'll look at this code in a moment in VTS code.
‫And when we do, we'll start out with an array that looks like this.
‫And then we'll run selection sort and sort it.
‫So now let's flip over and take a look at this.
‫So there is our selection sort function there and I'm going to scroll up and in our main function this
‫creates that array that we just saw and this line calculates the size of the array and that we call
‫selection sort on the array with the size.
‫And this for loop will loop through that array after it is sorted and print that out.
‫So now I'll run this and you can see that the array has now been sorted one through six.
‫And that is our function for selection sort.