Now that we are comfortable with selection so we can begin implementing our own solution to the algorithm.
So to start off, we know that we need a vector that we are going to want to sort.
So we will say let mute array and we will give it a type of vector storing it, and then we will create
it.
And we want to use the same values that we used in the previous lecture.
That way we can verify our results and understand how our algorithm was able to sort the values in ascending
order.
So something else that I want to do is I want to print out the values before and after we sort just
to make sure that everything was done properly.
So we have before sorting and we were going to pass in our array and we'll copy and paste this and we
will change this to after sorting.
So you can see by my print line statements that I am using the same variable and every location.
So that means that I am going to use selection sort and I'm going to pass an immutable reference to
our array.
So now we can begin implementing in our function called selection sort.
So we're going to pass in our array.
We know it's going to be a mutable reference of a vector that is of Type II, and we want to return
our vector that is an AIA that contains eight values.
So we looked at the pseudo code and we knew that we had two for loops.
So I will go ahead and start with the first one.
So we will start at index zero and go to our array length
minus one, which is following what we did in our pseudo code.
And now we want our smallest variable or value excuse me to be equal to I, and we need it to be mutable.
Because remember, if an index is less than our current smallest, then we need to update our current
smallest value.
So therefore it needs to be mutable.
That way we are able to update it.
So now we're going to do I plus one all the way to our array dot length again.
But this time we're going to go to the end of the array.
And so now we can do our checks.
So if array.
Of J is less than our smallest value.
Excuse me.
Is less than the array of smallest.
Then we want to set smallest equal to J.
And now once we exit out of the loops, we want to swap.
And luckily there is a swap function for us and the vector methods.
So we want to swap our smallest and our eye value and now we want to return our array and we have to
say array to VEC.
So now we have our array and our selection sort set up.
So let's execute this code to make sure it runs correctly.
So we see before sorting we have four, three, one and two, and then after sorting we have one, two,
three, four.
So that looks correct.
Let's do one more test case to make sure everything looks good.
So we will say let mute array two vec i eight equals two vec and let's do our second test case that
we did in the previous lecture.
So we have five, ten, one, four and 11 and now we want to print out the same messages as before,
before sorting
and we're going to do our second array.
We will call selection sort on a mutable reference again to our array two and now we will do our print
line again and say after sorting.
Hurray, too.
So we now expect this to be printing out to a raise for us.
So we have our first array, which we already know is correct, and now we have our second array of
five, ten, one, four and 11.
And we see that after running our selection sort algorithm, we have one, four, five, ten and 11.
So we are confident that our selection sort algorithm is correct and is correctly sorting the vectors
we pass in into ascending order.
So in to increasing order.
So we are confident that our solution is correct.
So in the next section we will continue on learning about additional sorting methods.
And remember, if you have any questions at any point during any of the lectures, please feel free
to ask them in the Q&A section.