‫So now we're going to do the second video for writing our merge code.
‫So I'm going to bring up this array that we saw in the last video with the two sorted sub arrays, and
‫I'm going to shrink it down to the bottom.
‫So in the last video, we created a new array called Left Array that had the values of the left sub
‫array.
‫And then we created another array called right array with the items from the right sub array.
‫And because all of the values in the original array are going to be overwritten, I'm going to color
‫the original array gray.
‫So we got to this point in the last video with this code, and I want to emphasize that all of the code
‫that we're writing in this video is just adding to this function.
‫So I'm going to bring in the first additional line here.
‫We'll set index to be equal to left index and left index as the first item in that array.
‫So index will point to that and then we'll set I to be equal to zero.
‫That will keep track of where we are in this array.
‫J will also be set equal to zero and that will keep track of where we are in this array.
‫So we'll bring in this wild loop.
‫And this wild loop will run until one of the two arrays on the bottom is empty.
‫And then inside of this wild loop, we'll check to see if left array at the index of AI is less than
‫or equal to right array at the index of J.
‫So I'm going to highlight the two items on the bottom that we're comparing.
‫And in this iteration of the wild loop, the conditional in this if statement will be true.
‫So then what we'll do is say array at index is equal to left index at the index of I and that just moves
‫this up here.
‫But now we need to move a couple of those arrows forward.
‫First we'll say index plus plus and that moves that index arrow over one and then we'll say I plus plus.
‫And that does the same thing for the I arrow.
‫So we're running out of room here.
‫So I'm going to move the wild loop up.
‫And in the next iteration of this wild loop, when we compare the items in the left and right array,
‫now it's the item in the right array that is less than so.
‫Now the conditional in this if statement is going to be false.
‫So now we'll say else, and now we'll set array at index to be equal to right array at the index of
‫J like this.
‫And just like we had in the if statement, we're going to have to iterate, index and move that over
‫and then we'll have to move J over as well.
‫We'll say J plus plus and that moves that over.
‫So now let's run through this wild loop to the end.
‫We move up three, four, five and six.
‫But now, because the right array is empty, it breaks us out of this wild loop.
‫So I'm going to remove this and we'll need a separate while loop to finish looping through the left
‫array.
‫So we'll do that like this and we'll just set a ray index to be equal to left a ray at the index of
‫AI and that moves that up.
‫And then we need to iterate index and that moves that arrow over and do the same thing for AI and that
‫moves that over and then we'll run through this wild loop one more time and that copies that up to the
‫original array.
‫So in this case, we had items that were still left and the left array.
‫It could work out that we have items left and the right array.
‫So we'll create an almost identical while loop to deal with that situation just like this.
‫And this will deal with a situation where we still have items left and the right array.
‫Now let's bring in the rest of the code that we wrote in this video.
‫And remember, this is all being added to the merge function that we started writing and the last video.
‫So now let's flip over to VS Code and look at all of the code together.
‫So here is the code that we wrote in the last video, and then I'll scroll up and in this video we created
‫these three variables and this is the wild loop that runs until one of the two arrays, left or right,
‫becomes empty.
‫And I'll scroll up again.
‫And then once one of the two arrays is empty, will run one of these two wild loops to finish iterating
‫through the remaining items.
‫So let's scroll up one more time.
‫And then in our main function we have our array that contains a sorted left sub array and a sorted right
‫summary, and with this line will calculate the size of the array and the left index will be zero and
‫we'll use the size to calculate the right and mid index.
‫And with this line we'll run merge on that array with left, mid and right index.
‫And then with this four loop, we'll print that out.
‫So I'll run this.
‫And this has returned a sorted array one through eight.
‫And that is our function for merge.