The primary topics in this part of the specialization are: asymptotic ("Big-oh") notation, sorting and searching, divide and conquer (master method, integer and matrix multiplication, closest pair), and randomized algorithms (QuickSort, contraction algorithm for min cuts).

From the lesson

Week 1

Introduction; "big-oh" notation and asymptotic analysis.

Okay. So in this video, we'll get our first sense of what it's actually like to analyze an algorithm.

Â

And we'll do that by first of all reviewing a famous sorting algorithm, namely the Merge Sort algorithm.

Â

And then giving a really fairly mathematically precise upper bound on exactly how many operations

Â

the Merge Sort algorithm requires to correctly sort an input array.

Â

So I feel like I should begin with a bit of an apology.

Â

Here we are in 2012, a very futuristic sounding date. And yet I'm beginning with a really quite ancient algorithm.

Â

So for example, Merge Sort was certainly known, to John Von Neumann all the way back in 1945.

Â

So, what justification do I have for beginning, you know, a modern class in algorithms with such an old example?

Â

Well, there's a bunch of reasons.

Â

One, I haven't even put down on the slide, which is like a number of the algorithms we'll see, "Merge Sort" as an oldie but a goodie.

Â

So it's over 60, or maybe even 70 years old. But it's still used all the time in practice, because this really is one of the methods of choice for sorting.

Â

The standard sorting algorithm in the number of programming libraries. So that's the first reason.

Â

But there's a number of others as well that I want to be explicit about.

Â

So first of all, throughout these online courses, we'll see a number of general algorithm design paradigms ways of solving problems that cut across different application domains.

Â

And the first one we're going to focus on is called the Divide-and-Conquer algorithm design paradigm.

Â

So in Divide-and-Conquer, the idea is, you take a problem, and break it down into smaller sub problems which you then solve recursively, ...

Â

... and then you somehow combine the results of the smaller sub-problem to get a solution to the original problem that you actually care about.

Â

And Merge Sort is still today's the, perhaps the, most transparent application of the Divide-and-Conquer paradigm, ...

Â

... that will exhibit very clear what the paradigm is, what analysis and challenge it presents, and what kind of benefits you might derive.

Â

As for its benefits, so for example, you're probably all aware of the sorting problem.

Â

Probably you know some number of sorting algorithms perhaps including Merge Sort itself.

Â

And Merge Sort is better than a lot of this sort of simpler, I would say obvious, sorting algorithms, ...

Â

... so for example, three other sorting algorithms that you may know about, but that I'm not going to discuss here.

Â

If you don't know them, I encourage you to look them up in a text book or look them up on the web.

Â

Let's start with three sorting algorithms which are perhaps simpler, first of all is "Selection Sort".

Â

So this is where you do a number of passes through the way repeatedly, identifying the minimum of the elements that you haven't looked at yet, ...

Â

... so you're basically a linear number of passes each time doing a minimum computation.

Â

There's "Insertion Sort", which is still useful in certain cases in practice as we will discuss, but again it's generally not as good as Merge Sort, ...

Â

... where you will repeatedly maintain the invariant that prefix view of array, which is sorted version of those elements.

Â

So after ten loops of Insertion Sort, you'll have the invariant that whatever the first ten elements of the array are going to be in sorted order, ...

Â

... and then when Insertion Sort completes, you'll have an entire sorted array.

Â

Finally, some of you may know about "Bubble Sort", which is where you identify adjacent pairs of elements which are out of order, ...

Â

and then you do repeated swaps until in the end the array is completely sorted.

Â

Again I just say this to jog your memory, these are simpler sorts than Merge Sort, ...

Â

... but all of them are worse in the sense that they're lack in performance in general, which scales with N^2, ...

Â

... and the input array has N elements, so they all have, in some sense, quadratic running time.

Â

But if we use this non-trivial Divide-and-Conquer approach, or non-obvious approach, we'll get a, as we'll see, a much better running time than this quadratic dependence on the input.

Â

Okay? So we'll get a win, first sorting in Divide-and-Conquer, and Merge Sort is the algorithm that realizes that benefit.

Â

So the second reason that I wanna start out by talking about the Merge Sort algorithm, is to help you calibrate your preparation.

Â

I think the discussion we're about to have will give you a good signal for whether you're background's at about the right level, of the audience that I'm thinking about for this course.

Â

So in particular, when I describe the Merge Sort algorithm, you'll notice that I'm not going to describe in a level of detail that you can just translate it line by line into a working program in some programming language.

Â

My assumption again is that you're a sort of the programmer, and you can take the high-level idea of the algorithm, how it works, ...

Â

... and you're perfectly capable of turning that into a working program in whatever language you see fit.

Â

So hopefully, I don't know, it may not be easy the analysis of Merge Sort discussion.

Â

But I hope that you find it at least relatively straight forward, ..

Â

.. because as the course moves on, we're going to be discussing algorithms and analysis which are a bit more complicated than the one we're about to do with Merge Sort.

Â

So in other words, I think that this would be a good warm-up for what's to come.

Â

Now another reason I want to discuss Merge Sort is that our analysis of it will naturally segment discussion of how we analyze the algorithms in this course and in general.

Â

So we're going to expose a couple of assumptions in our analysis, we're focus on worst case behavior, ...

Â

... or we'll look for guarantees on performance on running time that hold for every possible input on a given size, ...

Â

and then we'll also expose our focus on so called "Asymptotic Analysis", which meaning will be much more concerned with the rate of growth on an algorithms performance than on things like low-order terms or on small changes in the constant factors.

Â

Finally, we'll do the analysis of Merge Sort using what's called as "Recursion-Tree" method.

Â

So this is a way of tying up the total number of operations that are executed by an algorithm.

Â

And as we'll see a little bit later, this Recursion-Tree method generalizes greatly.

Â

And it will allow us to analyze lots of different recursive algorithms, lots of different Divide-and-Conquer algorithms, including the integer multiplication algorithm that we discussed in an earlier segment.

Â

So those are the reasons to start out with Merge Sort.

Â

So what is the computational problem that Merge Sort is meant to solve?

Â

Well, presumably, you all know about the sorting problem. But let me tell you a little bit about it anyways, just so that we're all on the same page.

Â

So, we're given as input. An array of N numbers in arbitrary order, and the goal of course is to produce output array where the numbers are in sorted order, let's say, from smallest to largest.

Â

Okay so, for example, we could consider the following input array, and then the goal would be to produce the following output array.

Â

Now one quick comment. You'll notice that here in input array, it had eight elements, all of them were distinct, it was the different integers, between 1 and 8.

Â

Now the sorting problem really isn't any harder if you have duplicates, in fact it can even be easier, ...

Â

... but to keep the discussion as simple as possible let's just, among friends, go ahead and assume that they're distinct, for the purpose of this lecture.

Â

And I'll leave it as an exercise which I encourage you to do, which is to think about how the Merge Sort algorithm implementation and analysis would be different, if at all, if there were ties, okay?

Â

Go ahead and make the distinct assumption for simplicity from here on out.

Â

Okay, so before I write down any pseudo code for Merge Sort, let me just show you how the algorithm works using a picture, ...

Â

... and I think it'll be pretty clear what the code would be, even just given a single example.

Â

So let's go ahead and consider the same unsorted input array that we had on the previous slide.

Â

So the Merge Sort algorithm is a recursive algorithm, and again, that means that a program which calls itself and it calls itself on smaller sub problems of the same form, okay?

Â

So the Merge Sort is its purpose in life is to sort the given input array. So it's going to spawn, or call itself on smaller arrays.

Â

And this is gonna be a canonical Divide-and-Conquer application, where we simply take the input array, we split it in half, we solve the left half recursively, we solve the right half recursively, and then we combine the results.

Â

So let's look at that in the picture.

Â

So the first recursive call gets the first four elements, the left half of the array, namely 5, 4, 1, 8. And, of course, the other recursive call is gonna get the rest of the elements, 7, 2, 6, 3.

Â

You can imagine these has been copied into new arrays before they're given to the recursive calls.

Â

Now, by the magic of recursion, or by induction if you like, the recursive calls will do their task.

Â

They will correctly sort each of these arrays of four elements, and we'll get back sorted versions of them.

Â

So from our first recursive call, we receive the output, 1, 4, 5, 8, and from the second recursive call, we received the sorted output, 2, 3, 6, 7.

Â

So now, all the remains to complete the Merge Sort is to take the two results of our recursive calls, these two sorted elements of length-4, and combine them to produce the final output, namely the sorted array of all eight of the input numbers.

Â

And this is the step which is called "Merge".

Â

And hopefully you are already are thinking about how you might actually implement this merge in a computationally efficient way.

Â

But I do owe you some more details. And I will tell you exactly how the merge is done.

Â

In effect, you just walk pointers down each of the two sort of sub-arrays, copying over, populating the output array in the sorted order.

Â

But I will give you some more details in just a slide or two.

Â

So that's Merge Sort in a picture.

Â

Split it in half, solve recursively, and then have some slick merging procedure

Â

to combine the two results into a sorted output.

Â

Explore our Catalog

Join for free and get personalized recommendations, updates and offers.