Docsity

Comparing Quicksort and Mergesort: An In-depth Analysis, Study notes of Programming Abstractions

Toggle sidebar
bg1
ProgrammingAbstractions-Lecture16
Instructor (Julie Zelenski):There must be endless toil and travail. Let’s ask another
question. So [inaudible] was the biggest thing he had done so far. [Inaudible] on the trail.
Not really anything new, but definitely the prospect of getting it all working and bigger
aspects of its own [inaudible] challenges.
So I expect to take a little bit more, but I'm always open to figure out what really
happened there. So let’s, we’ll start with something ridiculous, but just in case somebody
was just super hot, did anybody manage to finish [inaudible] with less than ten hours, a
total start to finish – hey I like to see that.
That’s a very good sign. Ten to fifteen – a very healthy sized group there. Fifteen to 20 –
there’s some people there. More than 20; okay, good, good and hopefully when you got
done you felt like, “Okay, I have accomplished something good,” but there’s something
really satisfying about writing a program.
The best thing you can’t do as well as the program you’ve just written, the idea that
taking in your own brain and cracking into it to write a program that can actually play a
game and beat you is really a pretty neat little twist on things.
So, hopefully that was an enjoyable and satisfyable place to get to right before the
midterm here. We’re gonna take a little break in assignments meaning I'm not giving you
an assignment now because what I really want you to spend your weekend doing is
enjoying the sunshine for five-ten minutes at a time and then prepping yourself for the
midterm which is coming up Tuesday night.
So we don’t have class on Monday and so the next time I’ll see you is Tuesday, 7:00 to
9:00 over in [inaudible] Auditorium and certainly if you have questions that need
answered, sending an email over the weekend is totally fine, and we’re showing up in the
Lair; we’ll have Lair hours on Monday evening [inaudible].
Student:[Inaudible].
Instructor (Julie Zelenski):No, we’re not. Try again. Okay, now we got my phone. So I
will probably put up [inaudible] in advance of the midterms, so if you actually wanted to
pick it up before the midterm or right after the midterm to get started on it, and it will be
due the following week and it’s a little bit lighter of a week than [inaudible] was just in
terms of planning.
What we’re going to pick up today is we’re going to talk Quick Sort which is the last of
the sorting algorithms I want to talk through and then I'm actually going to go through the
process of picking one of the sources, in this case [inaudible] and turning it into a fully
generic sorting template as kind of capitalizing on the futures of C++. So the material
here is the last sections of Chapter 7, a little bit of material pulled out of Chapter 11,
which is the client callback stuff, is there.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Comparing Quicksort and Mergesort: An In-depth Analysis and more Study notes Programming Abstractions in PDF only on Docsity!

ProgrammingAbstractions-Lecture

Instructor (Julie Zelenski) :There must be endless toil and travail. Let’s ask another question. So [inaudible] was the biggest thing he had done so far. [Inaudible] on the trail. Not really anything new, but definitely the prospect of getting it all working and bigger aspects of its own [inaudible] challenges.

So I expect to take a little bit more, but I'm always open to figure out what really happened there. So let’s, we’ll start with something ridiculous, but just in case somebody was just super hot, did anybody manage to finish [inaudible] with less than ten hours, a total start to finish – hey I like to see that.

That’s a very good sign. Ten to fifteen – a very healthy sized group there. Fifteen to 20 – there’s some people there. More than 20; okay, good, good and hopefully when you got done you felt like, “Okay, I have accomplished something good,” but there’s something really satisfying about writing a program.

The best thing you can’t do as well as the program you’ve just written, the idea that taking in your own brain and cracking into it to write a program that can actually play a game and beat you is really a pretty neat little twist on things.

So, hopefully that was an enjoyable and satisfyable place to get to right before the midterm here. We’re gonna take a little break in assignments meaning I'm not giving you an assignment now because what I really want you to spend your weekend doing is enjoying the sunshine for five-ten minutes at a time and then prepping yourself for the midterm which is coming up Tuesday night.

So we don’t have class on Monday and so the next time I’ll see you is Tuesday, 7:00 to 9:00 over in [inaudible] Auditorium and certainly if you have questions that need answered, sending an email over the weekend is totally fine, and we’re showing up in the Lair; we’ll have Lair hours on Monday evening [inaudible].

Student: [Inaudible].

Instructor (Julie Zelenski) :No, we’re not. Try again. Okay, now we got my phone. So I will probably put up [inaudible] in advance of the midterms, so if you actually wanted to pick it up before the midterm or right after the midterm to get started on it, and it will be due the following week and it’s a little bit lighter of a week than [inaudible] was just in terms of planning.

What we’re going to pick up today is we’re going to talk Quick Sort which is the last of the sorting algorithms I want to talk through and then I'm actually going to go through the process of picking one of the sources, in this case [inaudible] and turning it into a fully generic sorting template as kind of capitalizing on the futures of C++. So the material here is the last sections of Chapter 7, a little bit of material pulled out of Chapter 11, which is the client callback stuff, is there.

I was actually totally planning on going to the café today until I got a call from my son’s preschool right before I came to class that says he has swollen lymph nodes and a fever, so I’ve got to go get a little guy after class, so I'm sorry that won’t happen today, but hopefully we’ll be back on track a week from today.

Okay. So this is the last thing we had talked about at the end of the merge sort was comparing a quadratic and N-squared sort, this left and right sort right here to the linear arrhythmic which is the N-log in kind that merge sort runs in, right, showing you that really way out-performing even on small values and getting the large values [inaudible] just getting enormous in terms of what you can accomplish with a N-log-in algorithm really vastly superior to what you’re gonna get out of a N-squared algorithm.

I said well, N-log-I told you without proof that you’re going to take on faith that I wouldn’t lie to you, that that is the best we can do in terms of a comparison based sort, but what we’re going to look for is a way of maybe even improving on those kinds of merge sort by kind of a one thing we might want to do is avoid that copying of the data out and back that merge sort does and maybe have some lower cost in factors in the process, right that can bring our times down even better.

So the sort we’re looking at is quick sort. And I think if you were gonna come up with an algorithm name, naming it Quick Sort becomes good marketing here so it kind of inspires you to believe actually that it’s gonna live up to its name, is a divide-and-conquer algorithm that takes a strategy like merge sort in the abstract which is the divide into two pieces and [inaudible] sort those pieces and then join them back together.

But whereas merge sort does an easy split hard join, this one flips it on its head and said what if we did some work on the front step in the splitting process and then that may give us less to do on the joining step and so the strategy for quick sort is to run to the pile of papers, whatever we’re trying to work at in a partitioning step is the first thing that it does and that’s the splitting step and that partitioning is designed to kind of move stuff to the lower half and the upper half.

So having some idea of what the middle most value would be, and then actually just walking through the pile of papers and kind of you know, distributing to the left and right portions based on their comparison to this middle-most element.

Once I have those two stacks – I’ve got A through M over here and N through Z over there, that we’re cursively sorting those takes place, kind of assuming my delegates do their work and in the process of re-joining them is really kind of simple. They kind of, you know, joined together with actually no extra work, no looking, no process there.

So the one thing that actually is is where all the trickiness comes into is this idea of how we do this partitioning. So I was being a little big disingenuous when I said well if I had this stack of exam papers and I know that the names, you know range over the alphabet A through Z then I know where M is and I can say well, that’s a good mid pint around to divide.

So after we did all the work, it moved everything less than the pivot to the left side of that, everything greater than the pivot to the right side and then the pivot will tell us where the division is and then we make two recursive calls to sort everything up to but not including pivot, to sort everything past the pivot to the end, and then those calls are operating on the array in place, so we’re not copying it out and copying it back. So in fact, there actually even isn’t really any joint step here.

We said sort the four things on the left, sort the seven things on the right and then the joining of them was where they’re already where they need to be so in fact we don’t have anything to do in the joined wall.

The tricky part is certainly in partition. The version of the partition we’re using here is one that kind of takes two indices, two fingers, you might imagine and it walks a one end from the stop position down and one from the start position over and it’s looking for elements that are on the wrong side.

So if we start with our right hand finger on the very end of the array, that first loop in there is designed to move the right hand down looking for something that doesn’t belong in left side, so we identified 45 as the pivot value. It says find something that’s not greater then 45. That’s the first thing we found coming in from the right downward that doesn’t belong, so only that first step.

So it turns out it takes it just one iteration to find that, and so okay, this guy doesn’t belong on the right-hand side. Now it does the same thing on the inverse on the left-hand side. Try to find something that doesn’t belong on the left-hand side.

So the left hand moves up. In this case, it took two iterations to get to that, to this 92. So now we have a perfect opportunity to fix both our problems with one swap, exchange what’s at the current position of left hand with right hand and now we will have made both of our problems go away and we’ll have kind of more things on the left and the right that belong there and then we can walk inward from there, you know, to find subsequent ones that are out of order.

So those two get exchanged and now right again moves into find that 8, left moves over to find that 74. We swap again. And as we just keep doing this, right, until they cross, and once they’ve cross, right then we know that everything that the right scanned over is greater than the pivot, everything in the left was less than and at that point, right we have divided it, right, everything that’s small is kind of in the left side of the vector. Everything that’s large in the right side.

So I swap these two and now I get to that place and I say okay, so now big things here, small things there. The last thing we do is we move the pivot into the place right between them and know that it is exactly located right in the slot where they cross.

Now I have two sub arrays to work on. So the return from partition in this case will be the index four here and it says Okay, go ahead and quick sort this front-most part and then

come back and do the second part. So in recursion, kind of think of that as being postponed and it’s waiting; we’ll get back to it in a minute, but while we work on this step, we’ll do this same thing.

It’s a little big harder to see it in the small kind of what’s going on, but in this case, right, it divided that because the pivot was 8 into 8 and the three things greater than the pivot. In this case, the 41 is the pivot so it’s actually going to move 41 over there. It’s going to have the left of that as it keeps working its way down, it gets smaller and smaller and it’s harder to see the workings of the algorithm in such a small case of it rearranging it, but we’ll get back to the big left half in a second.

And so now that that’s all been sorted, we’re revisiting the side that’s advancing above the pivot to get these five guys, six guys in the order, taking the same strategy. You pivot around 67 [inaudible] doing some swapping [inaudible] and we’ll see it in slightly bigger case to kind of get the idea, but very quickly there’s something very interesting about the way quick sort is working is that that partition step very quickly gets things kind of close to the right place.

And so now that we’ve done both the left and the right we’re done. The whole thing is done. Everything kind of got moved into the right position as part of the recursive, part of the sorting and doesn’t need to be further moved to solve the whole problem.

That first step of the partition is doing a lot of the kind of throwing things kind of left and right, but it’s actually quickly moving big things and small things that are out of place closer to where they’re gonna eventually need to go, and it turns out to be a real advantage in terms of the running time of this sort because it actually is doing some quick movement close to where it needs to be and that kind of fixes it up in the recursive calls that examine the smaller sub sections of the [inaudible].

Let me show you what it sounds like, because that’s really what you want to know. So let’s – it’s gonna kind of [inaudible]. So you can see the big partitioning happening and then it kind of jumbling things up and then coming back to revisit them, but you notice that quite quickly kind of all the small ones kind of got thrown to the left all.

All the large elements to the right and then the kind of process of coming back and so the big partition that you can see kind of working across them and then kind of noodling down. If I turn the sound on for it and I’ll probably take it down a little bit.

So the big partition steps making a lot of noise, right and moving things kind of quickly and it almost appears, you know, to hear this kind of random sort of it’s hard to identify what’s going on during the big partition, but then as you hear it make its way down the recursive tree it’s focusing on these smaller and smaller regions where the numbers have all been gathered because they are similar in value.

And you hear a lot of noodling in the same pitch range region as its working on the smaller and smaller sub arrays and then they definitely go from low to high because of

You think that quick sort very quickly moves stuff to where it goes and does a little bit of noodling. Merge sort does a lot of moving things away and moving them back and kind of like as you see it growing, you’ll see a lot of large elements that get placed up in the front, but then have to be moved again right, because that was not their final resting place.

And so merge sort does a lot of kind of movement away and back that tends to cost it overall, right. You know, a higher constant factor of the moves than something like quick sort does where it more quickly gets to the right place and then doesn’t move it again.

So that was all well and good. That was assuming that everything went perfectly. That was given our simplistic choice of the pivot, you can imagine that’s really assuming a lot right that went our way. If we look at a particularly bad split, let’s imagine that the number we have to pick is pretty close to, you know, one of the extremes.

If I were sorting papers by alphabet and if the one on top happened to be a C, right, well then I'm gonna be dividing it pretty lop sided, right, it’s just gonna be the As and the Bs on one side and then everything [inaudible] to the end on the other side.

If I got a split that was about 90/10, ninety percent went on one side, ten percent on the other, you would expect right for it to kind of change the running time of what’s going on here, so I get one tenth of them over here and the low half and maybe be nine-tenths in the right half. Let’s just say every time it always takes nine-tenths and moves it to the upper half, so I kept picking something that’s artificially close to the front.

These like will kind of peter out fairly quickly diving N by 10 and 10 and 10 and 10, eventually these are gonna peter out very soon, but this one arm over there where I keep taking 90 percent of what remains.

I had 90 percent, but I had 81 percent. I keep multiplying by nine-tenths and I'm still kind of retaining a lot of elements on this one big portion, that the depth of this tree isn’t gonna bottom out quite as quickly as it used to, but it used to the number of times you could divide N by 2, the log based 2 of N was where we landed.

In this case, I have to take N and multiply it by nine-tenths so instead of one half, right, to the K, it’s nine tenths to the K and it’s like how many iterations how deep will this get before it gets to the simple case and so solving for N equals ten-ninths to the K is taking the log-based ten-ninths of both sides, then K, the number of levels is the log-based ten- ninths of them.

We’re kind of relying on a fact of mathematics here though is that all algorithms are the same value, so log-based A of N and log-based B of N, they only differ by some constant that you can compute if you just rearrange the terms around. So in effect this means that there is a constant in front of this. It’s like a constant difference, the ratio between ten- ninths and 2 that distinguishes these, but it still can be logged based 2 of N in terms of Big O, right, where we can throw away those constants.

So even those this will perform, obviously in, you know, experimentally you would expect that getting this kind of split would cause it to work more slowly than it would have in a perfect split situation. The Big O [inaudible] is still gonna look in-log-in arrhythmic and [inaudible].

So that was pretty good to know. So it makes you feel a little bit confident, like sometimes it might be getting an even split and sometimes it might be getting one-third, two-thirds, sometimes it might be getting you know, nine-tenths, but if it was kind of in the mix still dividing off some fraction is still doing okay.

Now, let’s look at the really, really, really worst case split. The really, really worst case split right, it didn’t even take off a fraction. It just managed to separate one, but somehow the pivot here did a really terribly crummy job, right, of dividing it at all that in effect, all the elements were greater than the pivot or less than the pivot since they all landed on one side and the pivot was kind of by itself.

So starting with an input of size N, we sectioned off 1 and we had N minus 1 remaining. Well, the next time, we sectioned off another one, so this happened again and again and again. We just got really bad luck all the way through. Each of these levels is only making progress for the base case at a snails pace. Right, one element is being subtracted each subsequent level and that’s gonna go to a depth of N.

And so now if we’re doing N work across the levels, it isn’t quite N work because in fact, some of these bottom out. It’s still the Gaussian sum in the end is that we are got N levels doing N work each. We suddenly have what was a linear arrhythmic algorithm now performing quadritically. So in the class the selection sort, inscription sort in its worst- case behavior.

So quite a range whereas merge sort is a very reliable performer, merge doesn’t care. No matter what the order is, no matter what’s going on, it’s the same amount of work, it means that there’s some inputs that can cause quick sort to vary between being linear arrhythmic or being quadratic, and there’s a huge difference as we saw in our earlier charts about how those things will run for large values.

So what makes the worst case – given how we’re choosing a pivot right now is to take the first thing in the sub section to look at as the pivot, what’s the example input that gives you this?

Student: Sorted.

Instructor (Julie Zelenski) :If it’s already sorted. Isn’t that a charmer? Here’s a sorting algorithm. If you ask it to do something and in fact if you accidentally [inaudible] twice, you already had sorted the data and you said, “Oh, you did something,” and you passed it back to the sort, it would suddenly actually degenerate into its very worst case.

change it to totally sorted or reverse sorted, for that matter, and so they look like they’re doing a lot of work, a lot of work about nothing.

And you can see that quick sort really is still way back there. It has looked at the first 10 percent or so and is doing a lot of frantic partitioning that never moves anything. And visibly kind of traipsing it’s way up there. Merge sort meanwhile is actually on the beach in Jamaica with a daiquiri mocking quick sort for all those times that it had said it was so much better than it was. “Duh, it was already sorted; you Doofus.” Apparently it wasn’t listening.

Merge sort almost looked like it did nothing and that’s because it took the left half, which is the smallest half, moved it off, right, kind of copied it back and it ends up copying each element back to the same location it was already originally present in. There you go, quick sort taking its time and it if I ran it against, let’s say, insertion sort and selection sort, why not [inaudible] nothing better to do than to sort for you all day long.

So there insertion sort actually finishing very early because it does nothing. It sort of looked at them all and said they were already in the right order, merge sort doing a little bit more work to move everything back and forth in the same place.

But in this case, selection sort and quick sort here at the bottom and selection sort here at the top doing really roughly about the same work if you look at the sum total of the comparisons and moves and then time spent on those suddenly shows that yeah, in this input situation quick sort is performing like an N-squared sort, and obviously very similar constant factors to those present in selection sort, so really behaving totally quadratically in that worst-case input.

If I make it reverse sorted, it’s a little more interesting because you can kind of see something going on. Everybody kind of doing their thing. Insertion sort now kind of hitting its worst case, so it actually coming in dead last because it did so much extra moving, but still showing the kind of quadratic terms, right, that selection sort and quick sort are both having, but higher constant factors there, so taking almost twice as long because actually doing a little bit extra work because of that inverted situation.

[Inaudible] something big, just cause. We’ll put it back into random, back into full speed and kind of watch some things happen. So quick sort right, just sort of done in a flash, merge sort finishing behind it and ordinarily the quadratic sorts taking quite a much longer time than our two recursive sorts.

But it’s the random input really buying quick sort its speed. Is it gonna win – oh, come on. Oh, come on. Don’t you want selection sort to come behind? I always wanted it to come from behind. It’s kind of like rooting for the underdog. Yes.

And yet, just remember don’t mock insertion sort. What it sorted is the only one that recognizes it and does something clever about it. [Inaudible] sort is anti-clever about it, so it still can hold its head up and be proud. This kind of [inaudible].

I was thinking about like what algorithms, right, there’s a reason why there’s four that we talked about and there’s actually a dozen more. It’s like different situations actually produce different outcomes for different sorts and there are reasons that even though they might not be the best sort for all purposes, there are some situations where it might be the right one, so if you had it, as data said that you expect it to be mostly sorted, but had a few [inaudible] in it.

Like if you had a pile or sand papers that were sorted and you dropped them on the floor and they got scuffed up a little bit, but they’re still largely sorted, insertion sort is the way to go. It will take advantage of the work that was already done and not redo it or create kind of havoc the way quick sort would.

But quick sort, the last thing we need to tell you, though, about it is we can’t tolerate this. Like the way quick sort is in its classic form with this first element as pivot would be an unacceptable way to do this algorithm. So quick sort actually typically is one of the most standard element that’s offered in a library of programming languages, the sort element.

Well, it has to have some strategy for how to deal with this in a way that does not degenerate and so the idea is, what you need to do is just pick a different choice for the pivot, a little bit more clever, spend a little bit more time, do something that’s a little less predictable than just picking that first most element.

So to take it to the far extreme, one thing you could do is just computer the median, analyze the data and compute the median. It turns out there is a linear time algorithm for this that would look at every element of the data set once and then be able to tell you what the median was and that would guarantee you that 50/50 split.

So if you go find it and use it at every stage, you will guarantee it. Most algorithms don’t tend to do that. That’s actually kind of overkill for the problem. We want to get it to where it’s pretty much guaranteed to never get the worst case.

But we’re not concerned with it getting 50/50. It’s got 60/40 or something close enough, that actually, you know, 60/40, 70/30 and it was bopping around in that range it’d be fine, so the other two that are much more commonly used is some kind of approximation of the median with a little bit of guessing or something in it.

For example, median of three takes three elements and typically it takes three from some specified position, it takes the middle most element, the last element and the front element and it says, “Okay, given those three, we arrange them to find out what’s the middle of those three, and use that.”

If the data was already sorted, it turns out you’ve got the median, right because it wasn’t the middle most. If data was just in random position, then you’ve got one that you know, at least there’s one element on one side, right, and so the odds that that every single time would produce a very bad split is pretty low. There are some inputs that could kind of get it to generate a little bit, but it’s pretty foolproof in most ordinary situations.

it, but I will expose you to it in the assignment because I think it’s a little bit of a – it’s part of our history right, as a computer science student to be expose to. Anything else?

I'm gonna show you some things about function templates. Oh, these are some numbers; I forgot to give that. Just to say, in the end, right, which we saw a little bit in the analysis that the constant factors on quick sort are noticeable when compared to merge sort by a factor of 4, moving things more quickly and not having to mess with them again.

Student: Quick sort [inaudible] in totally random order?

Instructor (Julie Zelenski) :Yes, they are. Yes –

Student: So it doesn’t have them taking –

Instructor (Julie Zelenski) :So this is actually using a classic quick sort with no degenerate protection on random data. If I had put it in, sorted it in on that case, you would definitely see like numbers comparable to like selection sorts, eight hours in that last slot, right.

Or you [inaudible] degenerate protection, and it would probably have an in the noise a small slow down for that little extra work that’s being done, but then you’ll be getting in log and performance reliable across all states of inputs.

Student: So [inaudible] protection as it starts to even out time wise with merge sort, does it still –

Instructor (Julie Zelenski) :No, it doesn’t. It actually is an almost imperceptible change in the time because it depends on which form of it you use because if you use, for example the random or median of three, the amount of work you’re doing is about two more things per level in the tree and so it turns out that, yeah, over the space of login it’s just not enough to even be noticeable.

Now if you did the full median algorithm, you could actually start to see it because then you’d see both a linear partition step and a linear median step and that would actually raise the cause and factors where it would probably be closer in line to merge sort. Pretty much nobody writes it that way is the truth, but you could kind of in theory is why we [inaudible].

What I'm going to show you, kind of motivate this by starting with something simple, and then we’re gonna move towards kind of building the fully generic, you know, one algorithm does it all, sorting function.

So what I'm gonna first look at is flop because it’s a little bit easier to see it in this case, is that you know, sometimes you need to take two variables and exchange their values.

I mean you need to go through a temporary to copy one and then copy the other back and what not – swapping characters, swapping ends, swapping strings, swapping students, you know, any kind of variables you wanted to swap, you could write a specific swap function that takes two variables by reference of the integer, strain or character type that you’re trying to exchange and then copies one to a temporary and exchanges the two values.

Because of the way C++ functions work, right, you really do need to say, “It’s a string, this is a string, you know, what’s being declared here is a string,” and so you might say, “Well, if I need more than one swap, sometimes that swaps strings and characters, you know, my choices are basically to duplicate and copy and paste and so on. That’s not good; we’d rather not do that.

But what I want to do is I want to discuss what it takes to write something in template form. We have been using templates right, the vector is a template, the set is a template, the stack and que and all these things are templates that are written in a very generic way using a place holder, so the code that holds onto your collection of things is written saying, “I don’t know what the thing is; is it a string; is it an N; is it a car?”

Well, vectors just hold onto things and you as a client can decide what you’re gonna be storing in this particular kind of vector. And so what we’re gonna see is if I take those flat functions and I try to distill them down and say, “Well, what is it that any swap routine looks like?” It needs to take two variables by reference, it needs to declare a template of that type and it needs to do the assignment all around to exchange those values.

Can I write a version that is tight unspecified leaving a placeholder in there for what – that’s really kind of amazing, that what we’Q1: gonna be swapping and then let there be multiple swaps generated from that one pattern.

So we’re gonna do this actually in the compiler because it’s always nice to see a little bit a code happening. So I go over here and I got some code up there that I'm just currently gonna [inaudible] because I don’t want to deal with it right now. We’re gonna see it in a second. It doesn’t [inaudible].

Okay, so your usual swap looks like this. And that would only swap exactly integers. If I wanted characters I’d change it all around. So what I'm gonna change it to is a template. I'm gonna add a template header at the top and so the template header starts with a key- word template and then angle brackets that says, “What kind of placeholders does this template depend on?”

It depends on one type name and then I’ve chose then name capital T, type for it. That’s a choice that I'm going to make and it says in the body of the function that’s coming up where I would have ordinarily fully committed on the type, I'm gonna use this placeholder capital T, type instead.

And so it would be very annoying every time I need to do that to duplicate that code that there’s a real appeal toward writing it once, as a template and using it in multiple situations.

So this shows that if I swap, right, it infers what I meant and then this thing basically just shows what happened is when I said int equals four, [inaudible] use that pattern to generate the swap int where the type parameter, that placeholder has been filled in and established that it’s int for this particular version which is distinct from the swap for characters and swap for strings and what not.

So let me show you that version I talked about print, and this one I'm gonna go back to the compiler for just a second. Let’s say I want to print a vector, printing a vector it like iterating all the members and using the screen insertion to print them out and so here it is taking some vector where the elements in it are of this type name. In this case, I’ve just used T as my short name here.

The iterator looks the same; the bracketing looks the same. I see the end now and I’d like to be able to print vectors of strings, vectors of ints, vector of doubles with this one piece of code. So if I call print vector and I pas code vector events, all’s good; vector doubles, all’s good; vector strings, all’s good.

Here’s where I could get a little bit into trouble here. I’ve made this [inaudible] chord that has an X and a Y field. I make a vector that contains these chords. And then I try to call print vector of C. So when I do this, right, it will instantiate a versive print vector where the T has been matched up to, oh it’s chords that are in there; you’ve got a vector of chords.

And so okay, we’ll go through and iterate over the sides of the vector and this line is trying to say see out of a chord type. And struts don’t automatically know how to out put themselves onto a string, and so at this point when I try to instantiate, so the code in print vector is actually fine and works for a large number of types, all the primitive types will be fine, string and things like that are fine.

But if I try to use it for a type for which it doesn’t have this output behavior, right I will get a compiler error, and it may come as a surprise because a print vector appeared to be working fine in all the other situations and all the sudden it seems like a new error has cropped up but that error came from the instantiation of a particular version in this case, that suddenly ran afoul of what the template expected of the type.

The message you get, let’s say in X code looks like this, no match for operator, less than, less than in standard CL, vector element, operator [inaudible]. So I'm trying to give you a little clue, but in its very cryptic C++ way of what you’ve done wrong.

So it comes back to what the template has to be a little bit clear about from a client point of view is to say well what is it that needs to be true. Will it really work for all types or is

there something special about the kind of things that actually need to be true about what’s filled in there with a placeholder to make the rest of the code work correctly.

So if it uses string insertion or it compares to elements using equals equals, right, something like a strut doesn’t by default have those behaviors and so you could have a template that would work for primitive types or types that have these operations declared, but wouldn’t work when you gave it a more complex type that didn’t have that data support in there.

So I have that peace code over here. I can just show it to you. I just took it down here. This is print vector and it’s a little bit of template. And if I were to have a vector declared of ints, I say print vector, V that this compiles, there’s nothing to print. It turns out it’ll just print empty line because there’s nothing in there, but if I change this to be chord T and my build is failing, right, and the error message I'm getting here is no match for trying to output that thing and it tells me that all the things I can output on a string.

Well, it could be that you could output a [inaudible] but it’s not one of those things, right, chord T doesn’t have a built-in behavior for outputting to a stream. So this is going to come back to be kind of important because I'm gonna keep going here is that when we get to the sort, right, when we try to build this thing, we’re gonna definitely be depending on something being true about the elements, right, that’s gonna constrain what the type could be.

So this is selection sort in its ordinary form, nothing special about it other than the fact that I changed it as so sorting an int, it’s the sort of vector with placeholder type. So it’s [inaudible] with the template typed in header, vector of type and then in here, operations like this really are accessing a type and another type variable out of the vector that was passed and then comparing them, which is smaller to decide which index.

It’s using a swap and so the generic swap would also be necessary for this so that we need to have a swap for any kind of things we want to exchange. We have a template up there for swap, then the sort can build on top of that and use that as part of its workings which is then able to come under swap to any two things can be swapped using the same strategy. And this is actually where templates really start to shine.

Like swap in itself isn’t that stunning of an example, because you think whatever, who cares if three lines a code, but every little bit does count, but once we get to things like sorting where we could build a really killer totally tuned, really high performance, you know, protection against a [inaudible] quick sort and we want to be sure that we can use it in all the places we need to sort.

I don’t to make it sort just strings and later when I need to sort for integers, I need to copy and paste it and change string to everywhere, and then if I find a bug in one, I forget to change it in the other I have the bug still lurking there.