﻿1
00:00:00,400 --> 00:00:07,570
‫So I'm going to break the code for the Merge helper function into two separate videos and I'm going

2
00:00:07,570 --> 00:00:10,690
‫to do that just because it is quite a bit of code.

3
00:00:10,900 --> 00:00:17,250
‫So I'm going to start out by bringing up our array that has two sorted sub arrays in it.

4
00:00:17,260 --> 00:00:24,640
‫And in this video, what we're going to do is start with this left side and create this array and then

5
00:00:24,640 --> 00:00:28,510
‫go to the right side and create this other array.

6
00:00:28,840 --> 00:00:32,380
‫And this is all we'll do in this video.

7
00:00:32,380 --> 00:00:41,050
‫In the next video, we'll write the code for moving these values back up into the original array.

8
00:00:41,530 --> 00:00:46,540
‫So let's put this array back the way it was and start writing our code.

9
00:00:46,840 --> 00:00:52,540
‫So we're going to pass it the array and then we're going to pass it these three indexes.

10
00:00:52,540 --> 00:00:59,890
‫So left index in this case is going to be this first item, mid index is going to be this item and then

11
00:00:59,890 --> 00:01:03,160
‫right index will be the last item in the array.

12
00:01:03,640 --> 00:01:06,940
‫So then we're going to calculate the size of each sub array.

13
00:01:06,940 --> 00:01:11,140
‫So we'll say left array size equals mid index.

14
00:01:11,140 --> 00:01:22,330
‫That's this index, the index of three minus left index, the index of zero plus one is a size of four.

15
00:01:23,040 --> 00:01:27,780
‫Then we'll say right array size is equal to right index.

16
00:01:27,780 --> 00:01:32,070
‫That's the index of seven minus mid index.

17
00:01:32,070 --> 00:01:33,960
‫That's the index of three.

18
00:01:33,960 --> 00:01:38,130
‫Seven minus three is an array size of four.

19
00:01:38,520 --> 00:01:44,910
‫So now that we have the sizes of the two sub arrays, we can create the two new arrays.

20
00:01:45,480 --> 00:01:50,790
‫So we'll say left array has a size of left array size.

21
00:01:50,790 --> 00:01:57,030
‫And the new right array that we're going to create has a size of right array size.

22
00:01:57,540 --> 00:02:03,660
‫So now that we've created these two new arrays, we'll build a four loop.

23
00:02:03,660 --> 00:02:09,390
‫And the number of times this loop will run is the size of the left array.

24
00:02:09,900 --> 00:02:17,700
‫So I'll highlight the left sub array and then we'll say left array at the index of I, which starts

25
00:02:17,700 --> 00:02:21,440
‫out at zero, is equal to the array.

26
00:02:21,450 --> 00:02:27,060
‫This is the array that we pass the function at the index of left index.

27
00:02:27,060 --> 00:02:32,490
‫That's zero plus I and I starts out at zero.

28
00:02:32,550 --> 00:02:39,870
‫So in the first iteration, we are copying this value into the index of zero of that left array.

29
00:02:40,290 --> 00:02:44,010
‫And that will just loop through and add these other items.

30
00:02:44,400 --> 00:02:49,890
‫And then we'll move to the right sub array and we'll create this for loop.

31
00:02:49,980 --> 00:02:54,330
‫And the number of times it will run is the size of the right array.

32
00:02:54,930 --> 00:03:03,480
‫And then we'll say right array at the index of J is equal to array at mid index plus one mid index plus

33
00:03:03,480 --> 00:03:12,090
‫one will be this item plus j j is going to start out being equal to zero and that will copy that item

34
00:03:12,090 --> 00:03:19,260
‫into the index of zero for right array and that will loop through and add these items as well.

35
00:03:19,770 --> 00:03:23,730
‫And that is as far as we're going to get in this video.

36
00:03:24,060 --> 00:03:26,840
‫So we have the original array.

37
00:03:26,850 --> 00:03:30,720
‫This is the array that we pass to merge to begin with.

38
00:03:31,080 --> 00:03:36,840
‫And then we've created these two new arrays on the bottom left array and right array and then copied

39
00:03:36,840 --> 00:03:40,200
‫all these values into these two new arrays.

40
00:03:40,620 --> 00:03:48,810
‫So we'll pick up here in the next video where we'll copy all of these values into the original array.

41
00:03:49,550 --> 00:03:50,600
‫Like this.

42
00:03:51,110 --> 00:03:54,050
‫So for now, I'm going to put this back like this.

43
00:03:54,380 --> 00:03:58,460
‫And that concludes merge code part one.

