Introduction to Algorithms (SMA 5503) #5

Материал готовится, пожалуйста, возвращайтесь позднее

Today we're going to talk about sorting, which may not come as such a big surprise. We talked about sorting for a while, but we're going to talk about it at a somewhat higher level and question some of the assumptions that we've been making so far. And we're going to ask the question how fast can we sort? A pretty natural question. You may think you know the answer. Perhaps you do. Any suggestions on what the answer to this question might be? There are several possible answers. Many of them are partially correct. Let's hear any kinds of answers you'd like and start waking up this fresh morning. Sorry?

Theta n log n. That's a good answer. That's often correct. Any other suggestions? N squared. That's correct if all you're allowed to do is swap adjacent elements. Good. That was close. I will see if I can make every answer correct. Usually n squared is not the right answer, but in some models it is. Yeah? Theta n is also sometimes the right answer. The real answer is "it depends". That's the point of today's lecture. It depends on what we call the computational model, what you're allowed to do. And, in particular here, with sorting, what we care about is the order of the elements, how are you allowed to manipulate the elements, what are you allowed to do with them and find out their order. The model is what you can do with the elements.

Now, we've seen several sorting algorithms. Do you want to shout some out? I think we've seen four, but maybe you know even more algorithms. Quicksort. Keep going. Heapsort. Merge sort. You can remember all the way back to Lecture 1. Any others? Insertion sort. All right. You're on top of it today. I don't know exactly why, but these two are single words and these two are two words. That's the style. What is the running time of quicksort?

This is a bit tricky. N log n in the average case. Or, if we randomize quicksort, randomized quicksort runs in n log n expected for any input sequence. Let's say n lg n randomized. That's theta. And the worst-case with plain old quicksort where you just pick the first element as the partition element. That's n^2. Heapsort, what's the running time there? n lg n always. Merge sort, I hope you can remember that as well, n lg n. And insertion sort? n^2. All of these algorithms run no faster than n lg n, so we might ask, can we do better than n lg n?

And that is a question, in some sense, we will answer both yes and no to today. But all of these algorithms have something in common in terms of the model of what you're allowed to do with the elements. Any guesses on what that model might be? Yeah? You compare pairs of elements, exactly. That is indeed the model used by all four of these algorithms. And in that model n lg n is the best you can do. We have so far just looked at what are called comparison sorting algorithms or "comparison sorts". And this is a model for the sorting problem of what you're allowed to do.

Here all you can do is use comparisons meaning less than, greater than, less than or equal to, greater than or equal to, equals to determine the relative order of elements. This is a restriction on algorithms. It is, in some sense, stating what kinds of elements we're dealing with. They are elements that we can somehow compare. They have a total order, some are less, some are bigger. But is also restricts the algorithm. You could say, well, I'm sorting integers, but still I'm only allowed to do comparisons with them.