﻿1
00:00:00,520 --> 00:00:03,430
‫So now we're going to code bubble sort.

2
00:00:03,820 --> 00:00:06,850
‫So we'll pick up where we left off with this array.

3
00:00:06,850 --> 00:00:11,950
‫Except I'll shrink this down into the corner to make room for our code.

4
00:00:12,310 --> 00:00:14,720
‫So the function is called bubble sort.

5
00:00:14,740 --> 00:00:21,370
‫We'll pass it this array, but we're also going to pass this the size of the array.

6
00:00:21,730 --> 00:00:28,930
‫So if you're coming from another programming language like Java or Python or JavaScript in those programming

7
00:00:28,930 --> 00:00:36,760
‫languages, we don't have to pass this the size, but it is something that we have to do in C++ when

8
00:00:36,760 --> 00:00:38,320
‫we're working with arrays.

9
00:00:38,650 --> 00:00:43,780
‫And we'll talk a little bit more about this when we go over to VS Code to run this.

10
00:00:44,320 --> 00:00:51,370
‫So in the last video, I talked about how the first time we went through the array, we did five swaps

11
00:00:51,370 --> 00:00:57,610
‫and we have six items, then we did four swaps and three and so on.

12
00:00:57,940 --> 00:01:04,780
‫That means that one of our four loops has to be decremental, and that's going to be this first for

13
00:01:04,780 --> 00:01:05,290
‫loop.

14
00:01:05,650 --> 00:01:11,350
‫We start out with I being equal to the size minus one.

15
00:01:11,680 --> 00:01:19,270
‫So size in this case is six, which means I to begin with is going to be set to five and then we're

16
00:01:19,270 --> 00:01:25,390
‫going to decrement this for loop and the next time will be four and three and so on.

17
00:01:25,810 --> 00:01:31,180
‫And then we'll use I in the next for loop like this.

18
00:01:31,540 --> 00:01:37,930
‫So this four loop will run five times the first time and then four and three and so on.

19
00:01:38,320 --> 00:01:46,240
‫And then inside of this four loop, we'll say if array at the index of J and in the first iteration

20
00:01:46,240 --> 00:01:54,610
‫that will be this item if it is greater than array at the index of J plus one, which is this item,

21
00:01:54,970 --> 00:02:02,080
‫then we're going to want to swap these two items and we'll do that with these three lines of code.

22
00:02:02,380 --> 00:02:04,150
‫So let's walk through these.

23
00:02:04,180 --> 00:02:09,430
‫We'll say temp is equal to array at the index of J.

24
00:02:09,430 --> 00:02:14,350
‫So we're saying temp is equal to four in this case.

25
00:02:14,650 --> 00:02:17,470
‫And then we'll say array at the index of J.

26
00:02:17,500 --> 00:02:18,850
‫That's that first item.

27
00:02:18,850 --> 00:02:26,080
‫We're going to set that to be equal to array at the index of J plus one, which does this.

28
00:02:26,380 --> 00:02:32,140
‫And then we'll set array at the index of J plus one to be equal to temp.

29
00:02:32,650 --> 00:02:33,610
‫Like that.

30
00:02:33,610 --> 00:02:36,520
‫And that swaps those two items.

31
00:02:37,240 --> 00:02:40,570
‫And that is all of the code for bubble sort.

32
00:02:40,930 --> 00:02:47,410
‫If we continue running, these four loops will eventually make the entire array look like this.

33
00:02:47,830 --> 00:02:50,980
‫So we'll look at this code in a moment in VTS code.

34
00:02:50,980 --> 00:02:57,220
‫And when we do, we'll start out with an array that looks like this and then we'll sort it.

35
00:02:57,670 --> 00:03:00,940
‫So now let's flip over and take a look at this.

36
00:03:01,540 --> 00:03:04,540
‫So there is our bubble sort function there.

37
00:03:04,900 --> 00:03:06,610
‫And I'll scroll up.

38
00:03:06,850 --> 00:03:11,230
‫And in our main function, this is the array that we're going to sort.

39
00:03:11,650 --> 00:03:16,390
‫And we're going to use this line here to figure out the size of the array.

40
00:03:16,810 --> 00:03:25,150
‫So when you say size of my array, what is going to be returned is the number of bytes in the array.

41
00:03:25,600 --> 00:03:28,090
‫Integers are four bytes.

42
00:03:28,390 --> 00:03:33,670
‫So that's going to return 24 because we have six items in the array.

43
00:03:34,000 --> 00:03:38,080
‫So we have to divide this by one of the items in the array.

44
00:03:38,080 --> 00:03:41,830
‫So we'll say size of my array at the index of zero.

45
00:03:41,860 --> 00:03:45,250
‫One item in the array is going to be four bytes.

46
00:03:45,580 --> 00:03:48,670
‫And that will give us the size of the array.

47
00:03:49,060 --> 00:03:54,880
‫And the reason you have to do that is when you come down here and call the function bubble sort and

48
00:03:54,880 --> 00:03:56,980
‫we pass it my array.

49
00:03:57,010 --> 00:04:04,030
‫The only thing that you're passing this function when you pass it, my array is the address of this

50
00:04:04,030 --> 00:04:06,160
‫first item in the array.

51
00:04:06,640 --> 00:04:11,060
‫And when you pass it the array, you are not also passing the size.

52
00:04:11,080 --> 00:04:13,600
‫You have to do that separately.

53
00:04:13,990 --> 00:04:19,060
‫So you will see that we'll do something similar to this with all of our sorting algorithms.

54
00:04:19,480 --> 00:04:27,520
‫So I want to emphasize, when we run bubble sort on my array, we are not returning a new array.

55
00:04:27,670 --> 00:04:34,390
‫We are sorting this existing array in place and making this array sorted.

56
00:04:34,780 --> 00:04:41,410
‫So down here at the bottom of our main function, we have this for loop that is going to loop through

57
00:04:41,410 --> 00:04:46,090
‫my array and then output the values from that array.

58
00:04:46,760 --> 00:04:48,350
‫So now I'll run this.

59
00:04:49,130 --> 00:04:54,050
‫And you can see that my array has now been sorted one through six.

60
00:04:54,910 --> 00:04:56,710
‫And that is our function.

61
00:04:57,470 --> 00:04:58,790
‫For bubble sort.

