﻿1
00:00:00,410 --> 00:00:07,190
‫So now we're going to write our code for selection sort and we'll pick up where we left off in the last

2
00:00:07,190 --> 00:00:07,880
‫video.

3
00:00:08,000 --> 00:00:13,160
‫Except I'll shrink this down into the corner to make room for our code.

4
00:00:13,610 --> 00:00:20,840
‫So we'll pass this an array of integers, and then we'll also pass this the size of the array.

5
00:00:21,610 --> 00:00:28,420
‫And we'll start out with this for loop that iterates through the length of the array where AI is equal

6
00:00:28,420 --> 00:00:29,140
‫to zero.

7
00:00:29,140 --> 00:00:30,940
‫So I'm going to bring this up.

8
00:00:31,120 --> 00:00:35,050
‫AI is going to be this index of zero.

9
00:00:35,230 --> 00:00:41,530
‫And in the last video we started out by setting min index to be equal to zero.

10
00:00:41,800 --> 00:00:46,630
‫And we're going to do that by setting it to be equal to AI.

11
00:00:46,900 --> 00:00:53,470
‫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

12
00:00:53,470 --> 00:00:55,840
‫to I from the for loop.

13
00:00:56,020 --> 00:01:03,010
‫And now we'll create that second for loop and notice that we're setting J to be equal to AI plus one.

14
00:01:03,640 --> 00:01:05,470
‫And I'll bring this back up.

15
00:01:05,470 --> 00:01:11,530
‫We're starting out with AI being equal to zero and J is equal to AI plus one.

16
00:01:11,770 --> 00:01:18,730
‫So we started out with min index being equal to AI, which in this case is zero.

17
00:01:18,850 --> 00:01:27,190
‫But we want to change that if we come across a value that's less than the value that stored at min index

18
00:01:27,580 --> 00:01:32,950
‫and since two is less than four, obviously that is the case here.

19
00:01:33,160 --> 00:01:37,300
‫So we'll set min index to be equal to J.

20
00:01:37,930 --> 00:01:42,430
‫So let's shrink this down and bring back our code.

21
00:01:42,910 --> 00:01:52,360
‫So we'll say if the value at the index of J is less than the value at the index of min index, then

22
00:01:52,360 --> 00:01:55,960
‫we'll set min index to be equal to J.

23
00:01:56,590 --> 00:02:01,450
‫And now let's bring this back up and J is equal to one.

24
00:02:01,450 --> 00:02:05,140
‫So now men index is the index of one.

25
00:02:05,790 --> 00:02:11,280
‫And now that second for loop is going to move Jay through the end of the array.

26
00:02:11,610 --> 00:02:15,480
‫So when we move to the next item, six is not less than two.

27
00:02:15,510 --> 00:02:19,920
‫So we keep looping until we get to here and now.

28
00:02:19,920 --> 00:02:27,000
‫The item at the index of J is less than the item at the Index of Men Index.

29
00:02:27,300 --> 00:02:33,390
‫So we set men index to be equal to J, which is the index of four.

30
00:02:33,870 --> 00:02:37,770
‫And then we continue looping to the end of the array.

31
00:02:38,510 --> 00:02:42,770
‫So now we know the index that contains the minimum value.

32
00:02:42,770 --> 00:02:45,200
‫It's that the index of four.

33
00:02:46,060 --> 00:02:48,040
‫So now we'll swap these.

34
00:02:48,040 --> 00:02:51,670
‫And when we do this, we know that that first item is sorted.

35
00:02:51,670 --> 00:02:53,980
‫So I'll colour that in green.

36
00:02:54,520 --> 00:03:00,700
‫So let's shrink this down and we'll bring in the code that we use to swap those two items.

37
00:03:00,700 --> 00:03:03,220
‫It's going to be these three lines.

38
00:03:03,490 --> 00:03:09,610
‫This is very similar to what we did to swap two items in an array and bubble sort.

39
00:03:09,790 --> 00:03:15,340
‫So I'm not going to walk through these three lines of code because we covered this in bubble sort.

40
00:03:15,730 --> 00:03:19,450
‫So I'm going to bring this up and walk through this one more time.

41
00:03:19,450 --> 00:03:27,010
‫When we go through the four loop again, I is moved to that second item in the array and J is going

42
00:03:27,010 --> 00:03:28,660
‫to be I plus one.

43
00:03:29,460 --> 00:03:36,420
‫So we're going to set men index to be equal to AI, which in this case is the index of one.

44
00:03:37,190 --> 00:03:43,730
‫But you can just look at this and see as we move Jay through the array, there are no items that are

45
00:03:43,730 --> 00:03:45,320
‫less than two.

46
00:03:45,650 --> 00:03:50,690
‫So we're going to move this all the way through and we're not going to change men index.

47
00:03:51,230 --> 00:03:59,660
‫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.

48
00:04:00,140 --> 00:04:04,910
‫Now, that does mean that the item at the index of one is sorted, so I'll go ahead and colour that

49
00:04:04,910 --> 00:04:06,080
‫in green.

50
00:04:06,560 --> 00:04:12,440
‫But our three lines of code that we use for swapping items does not need to be run here.

51
00:04:12,950 --> 00:04:21,110
‫So we'll create an if statement that says, if I is not equal to men index, only then do we run these

52
00:04:21,110 --> 00:04:22,310
‫three lines of code.

53
00:04:23,080 --> 00:04:30,430
‫So now let's add this into the rest of our code, and that is all of the selection sort function.

54
00:04:30,790 --> 00:04:35,740
‫If we keep running through this, we'll end up with an array that looks like this.

55
00:04:36,160 --> 00:04:39,460
‫So we'll look at this code in a moment in VTS code.

56
00:04:39,460 --> 00:04:43,870
‫And when we do, we'll start out with an array that looks like this.

57
00:04:43,990 --> 00:04:47,620
‫And then we'll run selection sort and sort it.

58
00:04:48,070 --> 00:04:51,160
‫So now let's flip over and take a look at this.

59
00:04:52,400 --> 00:05:00,860
‫So there is our selection sort function there and I'm going to scroll up and in our main function this

60
00:05:00,860 --> 00:05:07,910
‫creates that array that we just saw and this line calculates the size of the array and that we call

61
00:05:07,910 --> 00:05:11,180
‫selection sort on the array with the size.

62
00:05:11,510 --> 00:05:16,760
‫And this for loop will loop through that array after it is sorted and print that out.

63
00:05:17,150 --> 00:05:23,660
‫So now I'll run this and you can see that the array has now been sorted one through six.

64
00:05:24,490 --> 00:05:28,240
‫And that is our function for selection sort.

