Video 4-1: §4.1 Mergesort
CS 566 – Efficient Algorithms (Winter 2025-26) · unit page
· YouTube
2025-11-03, 11:32
The transcript was generated automatically by YouTube's speech recognition and is offered as is. It may contain errors, in particular concerning technical terms, names and formulas.
0:02 Now let's take a quick look at MergeSort . We can manage that in the time we have . MergeSort is perhaps one of the oldest sorting methods and is based on the merge operation as a sub-step, in which I combine two sorted lists into one larger sorted list. And I'll draw this schematically in these pictures here. That means the height of the values is the value itself . From left to right is the position in the array. And schematically, I can merge two internally sorted lists into one large internally sorted list.
0:43 Let's just show an example here of how that can work. I have two sorted parts, usually called runs, because—well, where the name comes from isn't entirely clear, but they are internally sorted.
0:58 And I have a buffer space into which I write the result. Merging is generally difficult if you don't have extra space. So let's allow ourselves that here. How does merging work? I compare the first elements in both runs.
1:13 Because the runs are sorted, the smallest element in the entire collection of inputs here must be either this one or this one. All others in the run are, after all, larger. The smaller of them is this one here. That means I put this one into the output. There it is. And it has disappeared from the input. Then I compare the next two and so on and so forth. In any case, I only have two elements that are the next candidates for the next smaller, the next larger element in the output. Whichever one is smaller wins and goes into the output.
1:59 one of the runs is completely used up, then there might be some left from the other, which I can simply copy over without looking further, and then my input is sorted, or rather, the two parts have been merged into a sorted sequence.
2:12 But that is not yet a sorting algorithm . I already assumed pre-sorted things here. Okay, maybe once more so we are on the same page.
2:36 I haven't told you what Merge Sort is yet, but since you likely already know it, you can answer the question. So here it's about the entire algorithm . I have only shown you one merge step so far.
3:34 Very, very many options, we might not even be able to fit them all on the screen. Okay. It's relatively clear. You can of course still change your answer now, or not.
3:48 So the correct answer is n log n. In particular, Merge Sort is one of those algorithms, as teased earlier, that show that you can sort in n log n time. So, what does Merge Sort look like? I have only shown you how a merge step works. In fact, the rest is a simple recursive procedure.
4:10 We find the midpoint, we take our input , divide it symbolically into two parts in the middle, sort the left part recursively, sort the right part recursively, and then we have exactly what we need for merging. Two sorted sub-sections, the left and the right, and we transform that into the completely sorted version.
4:33 Since the merging works out of place, i.e., merging the two parts into a buffer, we still have to copy it back because our sorting interface was always in-place to the outside.
4:45 Okay. So far, so good for Merge Sort; that’s the boring part. Uh, what we still want to do is analyze Merge Sort. How fast is it? Why is it actually n log n in the end?
5:02 And one version that works quite well here is to count the number of read, write, and read operations we perform in this entire algorithm.
5:13 And that gives a nice recursion that depends on the input size because the algorithm is recursive. If I have at most one element, then I don't have any, I just do a return here , so I have zero cost.
5:27 In the other cases, I have uh two recursive calls for half the elements, rounded up and down. That comes from the fact that if I have an odd number, I have to round. And the 2n, those are the costs for a merge, because I read every element in the input once and then write it once. The number of comparisons is a bit more annoying to count, because it can happen that I get lucky and one run ends early , and then I save comparisons because I could simply copy over the remaining elements from the other run.
6:02 So, to make our lives easier here, we count the number of uh write and read operations or the number of comparisons in the worst case. Then the factor of two would be gone. But these write and read operations make sense for another reason; those are the memory transfers, and there you don't have a debate between best and worst case.
6:31 The recursion is a bit tricky. Because of this rounding up and down, it's not so clear how to solve it. It becomes much easier if we first only look at powers of two as input sizes.
6:45 Then what happens here is that the rounding up and down makes no difference . A power of two is always an even number So, this n divided by two becomes—if n was 2 to the power of k—it becomes 2 to the power of k minus one, and the two calls are the same, which means I just have a factor of two in front.
7:06 If I write that out by simply substituting the recursion over and over , you can immediately see that I have two times two here, 2 to the power of k plus one; those are these here. Then I have the first recursive call, where I have two times and now the same thing with 2 to the power of k minus one . That means I have the two from here and the two from here. There is this pre-factor, and the 2 to the power of k minus one is what I get when I set k to k minus one here. Then I do the whole thing again recursively. In the next step, I then have this here; I have collected three factors of two. One from here, one from substituting recursively again here, and one from there. In the process, however, k has become smaller by two. Accordingly, if you look at these summands here, they are all the same.
7:59 The pre-factors keep getting larger and the second factor keeps getting smaller , but by the same amount. This means they are all 2 to the power of k plus one This means I can easily summarize the whole thing as this term. And then I can substitute back; if n is 2 to the power of k, then k is log n. Accordingly, I have this solution here. And this is now exact, Okay, if you want, you could say, yes, that's a bit of a cheat, but if I have an input size that isn't a power of two , I can at least estimate its cost by imagining, so to speak, that I would take an array up to the next larger power of two and sort that instead of the real one. That just creates more effort. That means it still works as an upper bound.
9:09 Actually, you can solve this recursion exactly if you just throw a lot of clever discrete math at it. And the solution has this closed-form representation, namely the solution we also found, 2 n log n plus something times 2 n, and that something now has to do with what happens when I take the logarithm and then round up and down.
9:39 And I don't really want to show you how to arrive at that, but I wanted to mention that one can also solve such really nasty recursions more precisely.
9:47 And here is a plot of what this linear term looks like, so this prefactor is plotted here for various n, and as you can see, it is always quite small, ranging between 0 and 0.1, or 0.172, but it has this periodic behavior.
10:10 These jumps become larger the larger N gets. As for how you calculate that, you can look into that in more advanced courses . For now, we just want to note that you can indeed show a true upper bound without any tricks involving powers of two or anything else. And it is significantly sharper than just sticking a factor of two on there.
10:40 This is, of course, just for your personal enjoyment and you won't need to reproduce it in the exams. That was all for Mergesort. We now have the first algorithm that can sort in asymptotically optimal time. It is also a stable method; we will come back to that later. It explains what that is here, but I will return to it in detail in the following chapter.
11:09 It is also good from a practical standpoint because it works essentially sequentially, which is good for the cache hierarchy. The major drawback is that I need additional memory for this buffer. If I want to perform merging efficiently and quickly, I can really only realize it with extra memory. Open parenthesis, it is possible otherwise, but it is not really practically fast. Close parenthesis.
← Video 0: Welcome, teaching mode, overview, assessment, academic integity, Campuswire, Slido, exam question galleryVideo 6-7: §6.6 The Boyer-Moore algorithm →