Data Structures and Algorithms
Transcript
[MUSIC PLAYING] DAVID MALAN: Recall that an algorithm is just step-by-step instructions for solving some problem. Not unlike this problem here wherein I sought Mike Smith among the whole phone book of names and numbers. But up until now, we've really only focused on those step-by-step instructions and not so much on how the data we are searching is stored. Of course, in this version of that problem, it's stored here on paper, but in the digital world, it's of course not going to be paper, but 0's and 1's.
But it's one thing to say that the numbers and maybe even the names are stored ultimately as 0's and 1's, but where and how exactly? There's all those transistors and they're flipping on and off, but with respect to each other, are those numbers laid out left to right, top to bottom, are they all over the place? Let's actually take a look at that question now and consider how a computer leverages what are called data structures to facilitate implementation of algorithms. Indeed, how you lay out a computer's data inside of its memory has non-trivial impacts on the performance or efficiency of your algorithms, whereas the algorithm itself can be correct as we've seen, but not necessarily efficient logically. Both space and the representation underneath the hood of your data can also make a significant impact. But let's simplify the world first. And rather than focus on, say, a whole phone book of names and numbers,
let's focus just on numbers, and much smaller numbers that aren't even phone numbers, but just integers, and only save seven of them at a time. And I've hidden these seven numbers, if you will, behind these seven yellow doors. And so by knocking and opening, one of these doors will reveal one number at a time. And the goal at hand, though, is to find a very specific number, just like I sought one specific phone number before, this time I want to find the number 50 specifically. Well, where to begin? I'll go with the one closest to me and knock, knock, knock-- 15 is the number. So a little bit low. Let's proceed from there to see 23. We seem to be getting closer. Let's open this door next and-- oh, we seem to have veered down smaller, so I'm a little confused. But I have four doors left to check. So 50 is not there and 50 is not there and 50 is, in fact, there. So not bad. Within just six steps, have I found the number in question.
But of course, to be fair, there were only seven doors. So if we generalize that to say that there were n doors where n is just a number, well that was roughly n doors I had to open among the n doors just to find the one that I sought. So could I have done better? You know, my instincts like yours were perhaps to start at the left and move to the right, and we seem to be on a good path initially. We went from 15 to 23, and then darn it if 16 didn't throw a wrench in the works, because I expected it, perhaps naively, to be bigger and bigger as I moved right. But honestly, had I not told you anything-- and indeed I did-- then you wouldn't have known anything about these numbers other than maybe the number 50 is actually there. I told you nothing as to the magnitude or the size of any of the other numbers, let alone the order, but in the world of the phone book, of course, we were able to take for granted that those names were
sorted by the phone company for us-- from left to right, from A to Z. But in this case, if your data is just added to the computer's memory one at a time in no particular order, the onus is on you, the programmer or algorithm, to find that number you're interested in nonetheless. Now what was left here? And indeed 4 is even smaller than 50. So these seven doors were by design randomly assigned a number. And so you could do no better. I might have gotten lucky. I might not have gone with my initial instincts and touch the number 15 at left. I might have, effectively blinded, gone and touched 50 and just gotten lucky, and then it would have been just one step. But there's only a one in seven chance I would have been correct so quickly, so that's not really an algorithm that I could reproduce with the same efficiency again and again. So how can I do better? And how does the phone company enable us to do better?
Well they, of course, put in a huge amount of effort upfront to sort all of those names and associated numbers from left to right, from A to Z. And so that's a huge leg up for us, because then I can assume I can do divide and conquer or so-called binary search, dividing that phone book in two as implied by "bi" in "binary," having the problem again and again. But someone's got to do that work for us, be it the phone company or perhaps me with these numbers. So let's take one more stab at this problem, this time presuming that the seven doors in question do, in fact, have the numbers behind them sorted from left to right, small to big. So where to find the number 50 now? I have seven doors behind which are those same numbers, but this time they are sorted from left to right. And no skipping ahead thinking that, well, I remember all the other numbers, so I know immediately where 50 is. Let's assume for the moment that we don't know anything
about the other numbers other than the fact that they are sorted. Well, my inclination is not to start at the left with this first door, much like my inclination ultimately with that phone book was not to start with the first page, but the middle. And indeed, I'm going to go here to the middle of these doors and-- 16. Not quite the one I want. But if the doors are sorted now, I know that that number 50 is not to the left, and so I'm going to go to the right. Where do I go to the right? Well, I have three doors left, I'm going to follow the same algorithm and open that door in the middle and-- oh, so close. I found only, if you will, the meaning of life. So 42, though, is not the number I care about, but I do know something about 50-- it's bigger than 42. And so now, it's quite simply the case that-- aha, 50 is there, it's going to be in that last number. So whereas before took me up to six steps to find the number 50, and only then by luck did I find it where
it was because it was just randomly placed, now I spent 1, 2, 3 steps in total, which is, of course, fewer than six. And as these numbers of doors grow in size and I have hundreds or thousands of doors, surely it will be the case just like the phone book that having this problem again and again is going to get me to my answer if it's there in logarithmic instead of linear time, so to speak. But what's key to the success of this algorithm-- binary search-- is that the doors are not only sorted, but they are back-to-back-to-back. Now I have the luxury of feet and I can move back and forth among these numbers, but even my steps take me some amount of time and energy. But fortunately, each such step just takes one unit of energy, if you will, and I can immediately jump wherever I would like one step at a time. But a computer is purely electronic, and in the context of memory, doesn't actually need to take any steps.
Electronically a computer can jump to any location in memory instantly in so-called constant time. So just one step, that might take me several. And so that's an advantage a computer has and it's just one of the reasons why they are so much faster than us at solving so many problems. But the key ingredient to laying out the data for a computer to solve your problems quickly is that you need to put your data back-to-back-to-back. Because a computer at the end of the day, yes, stores only 0's and 1's, but those 0's and 1's are generally treated in units of, say, eight-- 8 bits per byte. But those bytes, when storing numbers like this, need those numbers to be back-to-back-to-back and not just jumbled all over the place. Because it needs to be the case that the computer is allowed to do the simplest of arithmetic to figure out where to look. Even I in my head am sort of doing a bit of math figuring out, well where's the middle?
Even though among few doors you can pretty much eyeball it quickly. But a computer's going to have to do a bit of arithmetic, so what is that math? Well if I have 1, 2, 3, 4, 5, 6, 7 doors initially, and I want to find the middle one, I'm actually just going to do what? 7 divided by 2, which gives me 3 and 1/2-- that's not an integer that's that useful for counting doors, so let's just round it down to 3. So 7 divided by 2 is 3.5 rounded down to 3 suggests mathematically that the number of the door that's in the middle of my doors should be that known as 3. Now recall that a computer generally starts counting at 0 because 0 bits represent 0 in decimal, and so this is door 0, 1, 2, 3, 4, 5, 6. So there's still seven doors, but the first is 0 and the last is called 6. So if I'm looking for number 3, that's 0, 1, 2, 3. And indeed, that's why I jumped to the middle of these doors, because I went very specifically to location 3.
Now why did I jump to 42 next? Of course, that was in the middle of the three remaining doors, but how would a computer know mathematically where to go, whereas we can just rather eyeball it here? Well if you've got 3 doors divided by 2, that gives me, of course, 1.5-- let's round that down to 1. So if we now re-number these doors, it's 0, 1, 2, because these are the only three doors that exist, well door 1 is 0, 1-- the 42, and that's how a computer would know to jump right to 42.
Of course, with just one door left, it's pretty simple. You'd needn't even do any of that math if there's just one, and so we can immediately access that in constant time. In other words, even though my human feet are taking a bit of energy to get from one door to another, a computer has the leg-up, so to speak, of getting to these doors even quicker, because all it has to do is a little bit of division, maybe some rounding, and then jump exactly to that position in memory. And that is what we call constant time, but it presupposes, again, that the data is laid out back-to-back-to-back so that every one of these numbers is an equal distance away from every other. Because otherwise if you were to do this math and coming up with the numbers 3 or 1, you have to be able to know where you're jumping in memory, because that number 42 can't be down here, it has to be numerically in order exactly where you expect.
And so in computer science and in programming is this kind of arrangement where you have doors or really data back-to-back-to-back known as what's called an array. An array is a contiguous block of memory wherein values are stored back-to-back-to-back-to-back-- from left to right conceptually, although of course, direction has less meaning once you're inside of a computer. Now it is thanks to these arrays that we were able to search, even something like a phone so quickly.
After all, you can imagine in the physical world, a phone book isn't all that unlike an array, albeit a more arcane version here, because its pages are indeed back-to-back-to-back-to-back from left to right, which is wonderful. And you'll recall when we searched a phone book, we were already able to describe the efficiency via which we were able to search it-- via each of those three algorithms. One page at a time, two pages at a time, and then one-half of the remaining problem at a time. Well it turns out that there's a direct connection even to the simplification of that same problem. If I have n doors and I search them from left to right, that of course might take me as many six, seven total steps or n if the number I'm seeking is all the way at the end. I could have gone two doors at a time, although that really would have gone off the rails with the randomly-sorted numbers, because there would have been no logic to just going left to right twice as
fast because I would be missing every other element never knowing when to go back. And in the case of binary search, my last algorithm where I started in the middle and found 16, and then started in the middle of that middle and found 42, and then started in the middle of the middle and found my last number, binary search is quite akin to what we did by tearing that problem in half and in half. So how did we describe the efficiency of that algorithm last time? Well we proposed that my first algorithm was linear, this straight line in red represented here by the label n, because for every page in the phone book, in the worst case you might need one extra step to find someone like Mike Smith. And indeed, in the case of these doors, if there's just one more door added, you might need one more step to find that number 50 or any other. Now I could, once those doors are sorted, go through them twice as fast, looking two doors at a time,
and if I go too far and find, say, 51, I could double-back and fix that mistake. But what I ultimately did was divide and conquer. Starting in the middle, and then the middle of the middle, and the middle of the middle of the middle, and that's what give me this performance. This so-called logarithmic time-- log base 2 event which if nothing else means that we have a different shape fundamentally to the performance of this algorithm. It grows so much more slowly in time even as the problem gets really big. And even off the screen here, imagine that even as n gets huge, that green line would not seem to be going very high even as the red and yellow ones do. So in computer science, there are actually formal labels we can apply to this sort of methodology of analyzing algorithms. When you talk about upper bounds, on just how much time an algorithm takes, you might say this-- big O, quite literally.
That an algorithm is in a big O of some formula. For instance, among the formulas it might be are these here-- n squared, or n log n, or n, or log n, or 1. Which is to say you can represent somewhat simply mathematically using n-- or really any other place holder-- as your value a variable that represents the size of the problem in question. So for instance, in the case of linear search, when I'm searching that phone book left to right or searching these doors left to right, in the worst case, it might take me as many as n steps to find Mike or that 50, and so we would say that that linear algorithm is in big O of n, which is just a fancier way of saying quite simply that it's indeed linear in time. But sometimes I might get lucky, and indeed in the best case, I might find Mike or 50 or anything else much faster, and computer scientists also have ways of expressing lower bounds on the running times of algorithms. Whereby in the best case, perhaps, an algorithm
might take only this much time and at least this much time. And we use a capitalized omega to express that notion of a lower bound, whereas again, a big O represents an upper bound on the same. So we can use these same formulas, because depending on the algorithm, it might indeed take n squared steps or just 1 or constant number thereof, but we can consider even linear search to having a lower bound, because in the best case, maybe Mike or maybe 50 or any other inputs of the problem just so happens to be at the very beginning of that book or those doors. And so in the best case, a lower bound on the running time of linear search might indeed be omega of 1 because you might just get lucky and take one step or two or three or terribly few, but independent of the number n. And so there, we might express this lower bound as well. Now meanwhile there's one more Greek symbol here, theta, capitalized here, which represents a coincidence of upper and lower bounds.
Whereby if it happens to be the case for some algorithm that you have an upper bound and a lower bound that are the same, you can equivalently say not both of those statements, but quite simply that the algorithm is in theta of some formula. Now suffice it to say, this green line is good. Indeed, any time we achieve logarithmic time instead of, say, linear time, we have made an improvement. But what did we presuppose? Well, we presupposed in both the case of the phone book and in the case of those doors that they were sorted in advance for us. By me in the case of the doors and by the phone company in the case of the book. But what did it cost me and what did it cost them to sort all of those numbers and names
just to enable us ultimately to sort logarithmically? Well let's consider that in the context of, again, some numbers, this time some numbers that I myself can move around. Here we have eight cups, and on these eight cups are eight numbers from 1 through 8. And they're indeed sorted from smallest to largest, though I could equivalently do this problem from largest to smallest so long as we all agree what the goal is. Well let me go ahead and just randomly shuffle some of these cups so that not everything is in order anymore, and indeed now they're fairly jumbled, and indeed not in the order I want, so some work needs to be done. Now why might they arrive in this order? Well in the case of the phone book, certainly new people are moving into a town every day, and so they're coming in not themselves in alphabetical order, but seemingly random, and it's up to the phone company to slot them into the right place in a phone book for the sake of next year's print.
And the same thing with those doors. Were I to add more and more numbers behind those doors, I'd need to decide where to put them, and they're not necessarily going to arrive for my input source in the order I want. So here, then, I have some randomly-ordered data, how do I go about sorting it quickly? Well, let's take a look at the first problem I see. 2 and 1 are out of order, so let me just go ahead and swap, so to speak, those two. I've now improved to the state of my cups, and I've made some progress still, but 2 and 6 seem OK even though maybe there should be some cups in between. So let's look at the next pair now. We have 6 and 5, which definitely are out of order, so let's switch those. 6 and 4 are the same, out of order. 6 and 3, just as much. 6 and 8 are not quite back-to-back, but there's probably going to be a number in-between, but they are at least in the right order, because 6, of course, is less than 8. And then lastly we have 8 and 7.
Let's swap those here and done-- or are we not? Well I've made improvements with every such swap, but some of these cups still remain out of order. Now these two are all set. 2 and 5 are as well, even though ultimately we might need some numbers between them, but 4 and 5 are indeed out of order. 3 and 5 just as much. 6 and 5 are OK, 7 and 6 are OK, and 8 and 7 as well. So we're almost done there, but I do see some glitches. So let's again compare all of these cups pairwise-- 1, 2; 2, 4-- oops, 4, 3, let's swap that. Let's keep going just to be safe. 4, 5; 5, 6; 6, 7; 7, 8. And by way of this process, just comparing cups back-to-back, we can fix any mistakes we see. Just for good measure, let me do this once more. 1, 2; 2, 3; 3, 4; 4, 5; 5, 6; 6, 7; 7, 8. Now this time that I've gone all the way from left to right checking that every cup is in order, I can safely conclude that these cups are sorted. After all, if I just went from left to right and did no work,
why would I presume that if I do that same algorithm again, I'd make any changes? I wouldn't, so I can quit at this point. So that's all fine and good, but perhaps we could have sorted these differently. That felt a little tedious and I felt like I was doing a lot of work. What if I just try to select the cups I want rather than deal with two cups at a time? Let's go ahead and randomly shuffle these again in any old order, making sure to perturb what was otherwise left to right.
And here we have now another random assortment of cups. But you know what I'm going to do this time? I'm just going to select the smallest I see. 2 is already pretty small, so I'll start as before on the left. So let's now check the other cups to see if there's something smaller that I might prefer to be in this location. 3, 1-- ooh, 1 is better, I'm going to make mental note of this one. 5, 8, 7, 6, 4-- all right, so 1 would seem to be the smallest number. So I'm going to go ahead and put this where it belongs, which is right here at the side. There's really no room for it, but you know what? These were randomly-sorted, let me just go ahead and evict whatever's there, too, and put 1 in it's place. Now to be fair, I might have messed things up a little bit, but no more so than I might have when I received these numbers randomly. In fact, I might even get lucky-- by evicting a cup,
I might end up putting it in the right place so it all washes out in the end. Now let's go ahead and select the next smallest number, but not bother looking at that first one anymore. So 3 is pretty small, so I'll keep that in mind. 2 is even smaller, so I'll forget about 3 and now remember 2. 5 is bigger, 8 and 7 and 6 and 4-- all right, 2 now seems to be the next smallest number I can select. I know it belongs there, but 3's already there, so let's evict 3 and there you go, I got lucky. Now I have 1 and 2 in the right place. Let's again select the next smallest number. I see 3 here, and again, I don't necessarily know as a computer if I'm only looking at one number at a time if there are, in fact, anything smaller to its side. So let's check-- 5, 8, 7, 6, 4-- nope. So 3 I shall select, and I got lucky, I'll leave it alone. How about the next smallest number? 5 is pretty small, but 8, 7, 6, 4 is even smaller. Let's select this one, put it in its place, evicting the 5
and putting it where there's room. 8 is not that small, but it's all I know now. But ooh-- 7 is smaller, I'll remember this. 6 is even smaller, I'll remember that, and it feels like I'm creating some work for myself. 5 is the next smallest, 8's in the way. We'll evict 8 and put 5 right there. 7 is pretty small, but 6 is even smaller, but still smaller than 8, so let's pick up 6, evict 7, and put 7 in its place. Now for good measure, we're obviously done, but I as the computer don't know that yet if I'm just looking at one of these cups or, if you will, doors at a time. 7's pretty small, 8 is no smaller, so 7 I've selected to stay right there in its place. 8 as well, by that same logic, is now in its right place. So it turns out that these two algorithms that I concocted along the way actually do have some formal semantics. In fact, in computer science, we'd call the first of those algorithms that thing here, bubble sort.
Because in fact, as you compare two cups side-by-side and swap them on occasion in order to fix transpositions, well, your largest numbers would seem to be bubbling their way up to the top, or equivalently, the smallest ones down to the end, and so bubble sort is the formal name for that algorithm. How might express this more succinctly than my voice over there? Well let me propose this pseudocode. There's no one way to describe this or any algorithm, but this was as few English words as I could come up with and still be pretty precise. So repeat until no swaps the following-- for i from 0 to n minus 2, if the i-th and i-th plus 1 elements are out of order, swap them. Now why this lingo? Well computational thinking is all about expressing yourself very methodically, very clearly, and ultimately defining, say, some variables or terms that you'll need in your arguments. And so here what I've done is adopt a convention.
I'm using i to represent an integer-- some sort of counter-- to represent the index of each of my cups or doors or pages. And here, we are adopting the convention, too, of starting to count from 0. And so if I want to start looking at the first cup, a.k.a. 0, I want to keep looking up, up to the cup called n minus 2, because if my first cup is cup 0, and this is then 1, 2, 3, 4, 5, 6, 7, indeed the cup is labeled 8, but it's in position 7.
And so this position more generally, if there are n cups, would be n minus 1. So bubble sort is telling me to start at 0 and then look up to n minus 2, because in the next line of code, I'm supposed to compare the i-th elements and the i-th plus 1, so to speak. So I don't want to look all the way to the end, I want to look one shy to the end, because I know in looking at pairs, I'm looking at this one as well as the one to its right, a.k.a. i plus 1. So the algorithm ultimately is just saying,
as you repeat that process again and again until there are no swaps, just as I proposed, you're swapping any two cups that with respect to each other are out of order. And so this, too, is an example more generally of smalling local problems and achieving ultimately a global result, if you will. Because with each swap of those cups, I'm improving the quality of my data. And each swap in and of itself doesn't necessarily solve the big picture, but together when we aggregate all of those smaller solutions have we assembled the final result. Now what about that second algorithm, wherein I started again with some random cups, and then that time I selected one at a time the number I actually wanted in place? I first sought out the smallest. I found that to be 1 and I put it all the way there on the left. And I then sought out the next smallest number, which after checking the remaining cups, I determined was 2.
And so I put 2 second in place. And then I repeated that process again and again, not necessarily knowing in advance from anyone what numbers I'd find. Because I checked each and every remaining cup, I was able to conclude safely that I had indeed found the next smallest element. And so that algorithm, too, has a name-- selection sort. And I might describe it pseudocode similar in structure but with different logic ultimately. Let me propose that we do for i from 0 to n minus 1,
where again, n is the number of cups, and 0 is by convention my first cup, and n minus 1, therefore, is my last. And what I then want to do is find the smallest element between the i-th element and the n-th plus-- at n minus 1. That is, find the smallest element between wherever you've begun and that last element, n minus 1. And then if-- when you've found that smallest element, you swap it with the i-th element. And that's why I was picking up one cup and another and swapping them in place-- evicting one and putting one where it belongs. And you do this again and again and again, because each time your incrementing 1. So whereas the first iteration of this loop will start here all the way left, the second iteration will start here, and the third iteration will start here. And so with the amount of problem to be solved is steadily decreasing until I have 1 and then 0 cups left. Now it certainly took some work to sort those n cups,
but how much work did it take? Well in the case of bubble sort, what was I doing on each pass through these cups? Well I was comparing and then potentially swapping each adjacent pair of cups, and then repeating myself again and again. Well if we have here n cups, how many pairs can you create which you then consider swapping? Well if I have n cups, I could seem to make 1, 2, 3, 4, 5, 6, 7 out of 8 pairs at a time, so more generally n minus 1. So on each pass here, it would seem that I'm comparing n minus 1 cups. Now how many passes do I need to ultimately make? It would seem to be roughly n, because in the worst case, these cups might be completely out of order. Which is to say, I might indeed do n things n minus 1 times, and if you multiply that out, I'm going to get some factor of n squared. But what about selection sort, wherein I instead looked through all of the cups, selecting first the smallest, and then repeating
that process for the next smallest still? Well in that case, I started with n cups, and I might need to look at all n, and then once I found that, I might instead look at n minus 1. So there, too, I seem to be summing something like n plus n minus 1 plus n minus 2 and so forth, so let's see if we can't now summarize this as well. Well let me propose more mathematically, that, say, with selection sort, what we've done is this. In looking for that smallest cup, I had to make n minus 1 comparisons.
Because as I identified the smallest cup I'd yet seen, I compared it to no more than n minus others. Now if the first selection of a cup took me n minus 1 steps but then it's done, the next lesson of the next smallest cup would have taken me only n minus 2 steps. And if you continue that logic with each pass, you have to do a little bit less work until you're left with just one very last cup at the end, such as 8. So what does this actually sum too? Well you might not remember or see it at first glance, but it turns out, particularly if you look at one of those charts at the back of a textbook, does this summation or series actually aggregate to n times n minus all divided by 2. Now this you can perhaps multiply out a bit more readily as in n squared minus n all divided by 2. And if we factor that out, we can now get n squared divided by 2 minus n divided by 2. Now which of these terms, n squared divided by 2 or n divided by 2,
tends to dominate the other? That is to say, as n gets larger and larger, which of these mathematical expressions has the biggest effect on the number of steps? Well surely it's n squared, albeit divided by 2, because as n gets large, n squared is certainly larger than n. And so what a computer scientist here would typically do is just ignore those lower-ordered terms, so to speak. And he would say with a figurative or literal wave of the hand, this is on the order of n squared this algorithm.
That isn't to say it's precisely that many steps, but rather as n gets really large, it is pretty much that n squared term that really matters the most. Now this is not a form of proof, but rather a proof by example, if you will, but let's see if I can't convince you with a single example numerically of the impact of that square. Well if we start again with n squared over 2 minus n over 2 and say n is maybe 1 million initially-- so not eight cups, not 1,000 pages in a book,
but 1 million numbers or any other element itself. What does this actually sum to? Well 1 million squared divided by 2 minus 1 million divided by 2 happens to be 500 billion minus 500,000, which of course is 499,999,500,000. Now I daresay that is pretty darn close to big O of n squared. Why? Well if we started with, say, 1 trillion then halved it and ended up with 499 billion, that's still pretty close. Now in real terms, that does not equal the same number of steps,
but it gives us a general sense it's on the order of this many steps, because if we plugged in larger and larger values for n, that difference would not even be as extreme. Well why don't we take a look now at these algorithms in a different form altogether without the physical limitation of me as the computer? Pictured here is, if you will, an array of numbers, but pictured graphically. Wherein we have vertical bars, and the taller the bar, the bigger the number it represents. So big bar is big number, small bar is small number, but they're clearly, therefore, unsorted. Via these number of algorithms we've seen, bubble sort and selection sort, what does it actually look like to sort of many elements? Let's take a look. In this tool where I proceed to choose my first algorithm, which shall be, say, bubble sort. And you'll see rather slowly that this algorithm is indeed comparing pairwise elements, and if--
and only if they're out of order, swapping them again and again. Now to be fair, this quickly gets tedious, so let me increase the animation speed here. And now you can rather see that bubbling up of the largest. Previously it was my 8 and my 7 and 6. Here we have 99, 98, 97, but indeed, those tallest bars are making their way up. So let's turn our attention next to this other algorithm, selection sort, to see if it looks or perhaps feels rather different. Here now we have selection sort each time going through the entire list looking for the smallest possible element. Highlighted in red for just a moment here is 9, because we have not yet until-- oh, now found a smaller element, now 2, and now 1. And we'll continue looking through the rest of the numbers just to be sure we don't find something smaller, and once we do, 1 goes into place. And then we repeat that process, but we do fewer steps now,
because whereas there are n total bars, we don't need to look at the leftmost now because it's sorted, we only need look at n minus 1. So this process again will repeat. We found 2. We're just double-checking that there's not something smaller, and now 2 is in its place. Now we humans, of course, have the advantage of having an aerial view, if you will, of all this data. And certainly a computer could remember more than just the smallest number it's recently seen. Why not for efficiency remember the two smallest numbers? The three smallest numbers? The four smallest numbers? That's fine, but that argument is quickly devolving into-- just remember all the original numbers. And so yes, you could perhaps save some time, but it sounds like you're asking for more and more space with which to remember the answers to those questions. Now this, too, would seem to be taking us all day. Even if we down here increase the animation speed,
it now is selecting those elements a bit faster and faster, but there's still so much work to be done. Indeed, these comparison-based sorts that are comparing things again and again and then redoing that work in some form to improve the problem still just tend to end up on the order of-- bingo, of n squared. Which is to say that n squared or something quadratic tends to be rather slow. And this is in quite contrast to our logarithmic time before, but that logarithm thus far was for searching, not sorting. So let's compare these two now side by side, albeit with a different tool that presents the same information graphically sideways. Here again we have bars, and small bar is small number, and big bar is big number, but here, they've simply been rotated 90 degrees. On the left here we have selection sort, on the right here bubble sort, both of whose bars are randomly sorted so that neither has an edge necessarily over the other.
Let's go ahead and play all and see what happens here. And you'll see that indeed, bubbles bubbling up and selection is improving its selections as we go. Bubble would seem to have won because selection's got a bit more work, but there, too, it's pretty close to a tie. So can we do better? Well it turns out we can, so long as we use a bit more of that intuition we had when we started thinking computationally and we divided and conquered, we divided and conquered. In other words, why not, given n doors or n cups or in pages, why don't we divide and conquer that problem again and again? In other words, in the context of the cups, why don't I simply sort for you the left half and then the right half, and then with two sorted halves, just interweave them for you together. That would seem to be a little different from walking back and forth and back and forth and swapping elements again and again. Just do a little bit of work here, a little bit
more now, and then reassemble your total work. Now of course, if I simply say, I'll sort this left half, what does it mean to sort this left half? Well, I dare say this left half can be divided into a left half of the left half, thereby making the problem smaller. So somehow or other, we could leverage that intuition of binary search, but apply it to sort. It's not going to be in the end quite as fast as binary search, because with sort, you have to deal with all of the elements, you can't simply tear half of the problem away because you'd be leaving half of your elements unsorted. But it turns out there's many algorithms that are faster than selection and bubble sort, and one of those is called merge sort. And merge sort leverage is precisely this intuition of dividing a problem in half and in half, and to be fair, touching all of those halves ultimately, but doing it in a way that's more efficient and less
comparison-based than bubble sort and selection sort themselves. So let me go ahead and play all now with these three sets of bars and see just which one wins now. And after just a moment, there's nothing more to say-- merge sort has already won, if you will, even though now bubble has, and now selection. And perhaps this was a fluke-- to be fair, these numbers are random, maybe merge sort got lucky. Let's go ahead and play the test once more with other numbers. And indeed it again is done. Let me play it one third and final time, but notice the pattern now that emerges with merge sort. You can see if you look closely the actual halving again and again. And indeed, it seems that half of the list get sorted, and then you re assemble it at the very end. And indeed, let's zoom in on this algorithm now and look specifically at merge sort alone. Here we have merge sort, and highlighted in colors as we do work is exactly the elements you're sorting again and again.
The reason so few of these bars are being looked at a time is because again, logically or recursively, if you will, are we sorting first the left half? But no, the left half of the left half. But no, the left half of the left half of the left half and so forth, and what this really boils down to ultimately is sorting eventually individual elements. But if I hand you one element and I say, please sort this, it has no halves, so your work is done-- you don't need do a thing. But then if you have two halves, each of size 1, there might indeed be work to be done there, because if one is smaller than the other or one is larger than the other, you do need to interleave those for me to merge them. And that's exactly what merge sort's doing here. Allow me to increase the animation speed and you'll see as we go, that half of the list is getting sorted at a time. It's not perfect and it's not perfectly smooth, because that's-- because half of the other elements are there,
but now are reemerging the two halves. And that was fast, but it finished faster indeed than would have been for bubble and selection sort, but there was a price being paid. If you think back to our vertical visualization of bubble sort and selection sort, they were doing all of their work in place. Merge sort seemed to be getting a little greedy on us, if you will, and that it was temporarily putting some of those bars down here, effectively using twice as much space as those first two algorithms, selection and bubble. And indeed, that's where merge sort gets its edge fundamentally. It's not just a better algorithm, per se, and better thought-out, but it actually additionally consumes more resources-- not time, but space. By using twice as much space-- not just the top half of the screen, but the bottom-- can merge sort temporarily put some of its work over here,
continue doing some other work, and then reassemble them together. Both selection sort and bubble sort did not have that advantage. They had to do everything in place, which is why we had to swap so many things so many times. We had far fewer spots in which to work on that table. But with merge sort, spend a bit more space, and you can reduce that amount of time. Now all of these algorithms assume that our data is back-to-back-to-back-- that is, stored in an array. And that's great, because that's exactly how a computer is so inclined to store data inherently. For instance, pictured here is a stick of memory of RAM-- Random Access Memory. And indeed, albeit a bit of a misnomer that R in RAM, random, actually means that a computer can jump in instant or constant time to a specific byte. And that's so important when we want to jump around our data, our cups, or our pages in order to get at data instantly, if you will.
And the reason it is so conducive to laying out information back-to-back contiguously in memory is if we consider one of these black chips on this DIMM-- or Dual In-line Memory Module-- is that we have in this black chip really, if you will, an artist's rendition at hand. That artist's rendition might propose that if you have some number of bytes in this chip, say 1 billion for 1 gigabyte, it certainly stands to reason that we humans could number those bytes from 0 on up-- from 0 to 1 billion, roughly speaking. And so the top left one here might be 0, the next one might be 1, the next one thereafter should be 2, and so we can number each and every one of our bytes. And so when you store a number on a cup or a number behind a door, that amounts to just writing those numbers inside of each of these boxes. And each is next to the other, and so with simple arithmetic, a bit of division and rounding, might you be able to jump instantly to any one of these addresses?

Ask anything
We've read this whole study set, so questions about it get the best answers.