1 00:00:07,380 --> 00:00:14,760 Now that we are comfortable with selection so we can begin implementing our own solution to the algorithm. 2 00:00:16,230 --> 00:00:22,570 So to start off, we know that we need a vector that we are going to want to sort. 3 00:00:22,590 --> 00:00:32,790 So we will say let mute array and we will give it a type of vector storing it, and then we will create 4 00:00:32,790 --> 00:00:33,060 it. 5 00:00:33,060 --> 00:00:37,770 And we want to use the same values that we used in the previous lecture. 6 00:00:37,800 --> 00:00:48,810 That way we can verify our results and understand how our algorithm was able to sort the values in ascending 7 00:00:48,810 --> 00:00:49,410 order. 8 00:00:49,860 --> 00:00:56,820 So something else that I want to do is I want to print out the values before and after we sort just 9 00:00:56,820 --> 00:01:00,630 to make sure that everything was done properly. 10 00:01:02,520 --> 00:01:10,740 So we have before sorting and we were going to pass in our array and we'll copy and paste this and we 11 00:01:10,740 --> 00:01:14,670 will change this to after sorting. 12 00:01:15,750 --> 00:01:21,350 So you can see by my print line statements that I am using the same variable and every location. 13 00:01:21,360 --> 00:01:30,360 So that means that I am going to use selection sort and I'm going to pass an immutable reference to 14 00:01:30,360 --> 00:01:31,170 our array. 15 00:01:32,130 --> 00:01:38,040 So now we can begin implementing in our function called selection sort. 16 00:01:38,340 --> 00:01:41,250 So we're going to pass in our array. 17 00:01:41,370 --> 00:01:49,770 We know it's going to be a mutable reference of a vector that is of Type II, and we want to return 18 00:01:49,770 --> 00:01:55,770 our vector that is an AIA that contains eight values. 19 00:01:56,490 --> 00:02:01,020 So we looked at the pseudo code and we knew that we had two for loops. 20 00:02:01,260 --> 00:02:03,990 So I will go ahead and start with the first one. 21 00:02:03,990 --> 00:02:10,020 So we will start at index zero and go to our array length 22 00:02:12,420 --> 00:02:16,770 minus one, which is following what we did in our pseudo code. 23 00:02:16,860 --> 00:02:25,980 And now we want our smallest variable or value excuse me to be equal to I, and we need it to be mutable. 24 00:02:25,980 --> 00:02:34,350 Because remember, if an index is less than our current smallest, then we need to update our current 25 00:02:34,350 --> 00:02:35,280 smallest value. 26 00:02:35,280 --> 00:02:37,140 So therefore it needs to be mutable. 27 00:02:37,140 --> 00:02:39,390 That way we are able to update it. 28 00:02:39,930 --> 00:02:49,230 So now we're going to do I plus one all the way to our array dot length again. 29 00:02:50,310 --> 00:02:52,830 But this time we're going to go to the end of the array. 30 00:02:54,660 --> 00:02:56,310 And so now we can do our checks. 31 00:02:56,310 --> 00:02:57,540 So if array. 32 00:02:58,780 --> 00:03:03,550 Of J is less than our smallest value. 33 00:03:04,180 --> 00:03:04,930 Excuse me. 34 00:03:04,930 --> 00:03:10,060 Is less than the array of smallest. 35 00:03:11,820 --> 00:03:16,680 Then we want to set smallest equal to J. 36 00:03:20,160 --> 00:03:25,460 And now once we exit out of the loops, we want to swap. 37 00:03:25,470 --> 00:03:31,080 And luckily there is a swap function for us and the vector methods. 38 00:03:31,080 --> 00:03:39,180 So we want to swap our smallest and our eye value and now we want to return our array and we have to 39 00:03:39,180 --> 00:03:42,240 say array to VEC. 40 00:03:43,110 --> 00:03:48,420 So now we have our array and our selection sort set up. 41 00:03:48,420 --> 00:03:52,260 So let's execute this code to make sure it runs correctly. 42 00:03:52,980 --> 00:04:00,960 So we see before sorting we have four, three, one and two, and then after sorting we have one, two, 43 00:04:00,960 --> 00:04:01,890 three, four. 44 00:04:01,980 --> 00:04:03,510 So that looks correct. 45 00:04:03,510 --> 00:04:09,300 Let's do one more test case to make sure everything looks good. 46 00:04:09,300 --> 00:04:18,210 So we will say let mute array two vec i eight equals two vec and let's do our second test case that 47 00:04:18,210 --> 00:04:19,560 we did in the previous lecture. 48 00:04:19,560 --> 00:04:30,510 So we have five, ten, one, four and 11 and now we want to print out the same messages as before, 49 00:04:30,510 --> 00:04:31,740 before sorting 50 00:04:37,710 --> 00:04:39,750 and we're going to do our second array. 51 00:04:41,430 --> 00:04:51,180 We will call selection sort on a mutable reference again to our array two and now we will do our print 52 00:04:51,180 --> 00:04:54,720 line again and say after sorting. 53 00:04:59,120 --> 00:05:00,090 Hurray, too. 54 00:05:00,630 --> 00:05:05,540 So we now expect this to be printing out to a raise for us. 55 00:05:05,550 --> 00:05:11,640 So we have our first array, which we already know is correct, and now we have our second array of 56 00:05:11,640 --> 00:05:15,510 five, ten, one, four and 11. 57 00:05:15,510 --> 00:05:23,400 And we see that after running our selection sort algorithm, we have one, four, five, ten and 11. 58 00:05:23,400 --> 00:05:33,810 So we are confident that our selection sort algorithm is correct and is correctly sorting the vectors 59 00:05:33,810 --> 00:05:36,120 we pass in into ascending order. 60 00:05:36,120 --> 00:05:38,760 So in to increasing order. 61 00:05:39,060 --> 00:05:43,290 So we are confident that our solution is correct. 62 00:05:43,500 --> 00:05:49,260 So in the next section we will continue on learning about additional sorting methods. 63 00:05:49,260 --> 00:05:54,780 And remember, if you have any questions at any point during any of the lectures, please feel free 64 00:05:54,780 --> 00:05:56,970 to ask them in the Q&A section.