1、Programming MethodologyLecture23Programming Methodology-Lecture23 Instructor (Mehran Sahami): So welcome to the 106A lecture just before Thanksgiving. I thought attendance might not be spectacular today. People are still filtering in, but it抯 good to see so many of you here, though. I抦 sure some of
2、you have already headed home. You may be wondering who I am. I would have thought before we got back mid-quarter evaluations that you stood a chance of recognizing me as the TA of your class, but the comment of more than half of the people who responded to the question of how is Ben doing was, 揑 don
3、抰 know Ben. I抳e never interacted with Ben. So I assume he抯 doing a great job.? Most of you jumped to that conclusion. But ?so I sort of chased at this, but not much. I realize, though, that I would enjoy the opportunity to develop a little more exposure. And so Maron and I decided to switch roles fo
4、r today. In fact, he抯 making copies as we speak, and there抯 going to be one handout that抯 sort of pertinent to what you抣l be doing for inaudible the next assignment. And he should be back any time, but I certainly can抰 blame him for being ?running a little late since I was supposed to make the copie
5、s before class myself. So anyway, in my capacity as Maron, I don抰 hope to be as spontaneous or as funny as he is, but I do hope to talk about something that is a little bit off of the sort of charted path of CS106A抯 curriculum. So one way of describing the things that you抮e learning in 106A is that
6、you抮e learning how to take the ways that you already know of doing things that may take you some time and maybe error-prone in the doing of them and tell a computer precisely how to do them so that you reap the benefits of a machine being able to execute those instructions. And they抣l be faster. The
7、re抣l be fewer errors as long as you get the instructions right in the first place. And that抯 got to be an exhilarating feeling. It抯 got to be empowering, or at least it was when I was sitting in your place as a freshman, to know that, all of a sudden, if you have a good idea ?if you can come up with
8、 a procedure for solving a problem that you抳e always had and tell a computer how to do it, then the computer will be able to do that over and over and over again. All right? So that抯 106A in a nutshell. So it抯 sort of ?it抯 not a perfect metaphor. What does it mean to run breakout by hands? I don抰 kn
9、ow. So what I ?the distinction I抦 trying to make between what you抳e been learning so far and what you will learn, hopefully, in this lecture ?and certainly if you go on to CS106B ?is that there are things that computers can do that you couldn抰 possibly have simulated by hand. Computers can handle so
10、 much more data than you could ever manage to sort through on your own manually that it抯 worth you抮e learning a bit about how to make them do that ?how to think about instructing a computer to do something that you couldn抰 possibly have done. So today, we抮e going to talk about two of the most import
11、ant kinds of those problems in computer science, and I抦 going to stop just talking at you and show you some examples in a second. But those two topics are searching and sorting. How do you find something in a set of things that you have? And how, if you could impose some structure on that set, would
12、 you both find it more quickly, and ?I guess I got ahead of myself ?how would you go about imposing the sort of structure that would help you find it more quickly? All right. So if I抦 seeming to ramble, that stops now. So searching and sorting ?I say they are important, and I hope you believe me, an
13、d you think that for that reason, it抯 worth talking about for an entire lecture. But they really are just a way of getting to talk about something a little deeper, which is this concept of algorithmic efficiency that we haven抰 said much about in the class so far. So that抯 the third part of this two-
14、part lecture. What抯 the deal with searching? Well, searching doesn抰 quite fit the mold as something you couldn抰 do by hand. You find things all the time. Chapter 12 of the book looks at two operations of the searching and sorting. This is pretty generic. All right. So searching is simpler. You can d
15、efine a search problem ?say you have an array or some other collection of things, and you have something you want to find. The typical way this is done is that you want to find the index into the array ?where that element was ?or, in a more generic case, you want to find out if that element is even
16、in the array. So you want to have some way of determining that it wasn抰 actually found. And that may be all you care about. It may be the case that, if the search routine returns anything other than negative one, you don抰 actually care what its value was. But we adopt ?I will adopt the convention fo
17、r this lecture that if you don抰 find what you抮e searching for, then the method that you wrote to do the search should return a negative value since that抯 not a valid index into the array. All right. So if you have a set of things that you抮e trying to search through, the easiest way of doing that is
18、just to look at all of them and see if each one, in turn, is what you抮e looking for. That抯 called a linear search because, to sort of pre-figure what we抮e going to talk about at the end of the lecture, the number of operations you have to do to find the element that you抮e looking for is linearly pro
19、portional to the number of elements that you had to begin with. Now it may not be obvious right now that there抯 something better that you can do, and with an arbitrary set of data, there is not anything better that you can do. But this procedure that I抳e written up here is an implementation of what
20、you already could have written ?a procedure for finding an element in an array of integers. So there抯 nothing very tricky about this. If it had been a string, you might have even avoided having to write the function and called it something like, 揑ndex of?with a character. And inside of 揑ndex of? tho
21、ugh you don抰 have to write the code, it would have had a four loop that iterated over each of the characters of the string, tested each one for quality with the key that you were looking for and then, presuming it found one that equaled that character ?or in this case, that integer ?it would return
22、the index of it. And if the four loop completes, then you must not have examined any elements that matched, and so you return negative one. Okay? So here抯 a simulation of how that would work, although the ?leaving nothing to the imagination here, this is pretty easy to sort of envision. So we have t
23、his array of primes, and it has 17 in it. So we are going to expect to find that this linear search shouldn抰 return negative one. But the second one, we抮e looking for 27 ?which sort of looks prime, but of course, it抯 not because it抯 nine times three. All right? Okay. So we called linear search, and
24、here抯 our new stat frame, which has the local variables I and the parameters P and array in it. This is sort of Eric Robert抯 ?who wrote the textbooks ?way of displaying the execution model of JAVA. All right? So we抮e just looping over it, and we抮e testing as I equals zero and then one and then two a
25、nd then three and four, five, six ?where should we find it? Well, we were looking for seven ?no. Right now, we抮e looking for 27. So we抮e just gonna go to the ?I think there was a problem with the way that ?well, anyway. The console here prints out that, indeed, we found 17 at position six. There may
26、 have been a problem converting PowerPoint slides to Keynote slides, so I apologize for the sort of shakiness of these animations. But the content of the slides should be adequate. Cool. All right. So now we抮e in our second called linear search, and the only thing that抯 changed at this point from th
27、e beginning of the first called was that the key is different. So I is still gonna start at zero, and it抯 still gonna iterate over all of the positions of the array. But we抮e gonna go through the entire array, which means going all the way up to ten where I is no longer less than ten. And we didn抰 f
28、ind 27 anywhere. So I hope this is all fairly clear. But these are the results that you would have expected. So we found the position of 17, and then we looked for 27 but didn抰 find it and returned negative one. Cool. How many people think they could have written that in their sleep? That that was t
29、oo much time to spend on such a simple idea? All right. Well, it gets more interesting from here on out, but ?so talk to me. What is the problem with linear search? Student: It takes a lot of time. Instructor (Mehran Sahami): It takes a lot of time, right? It takes as much time as you have elements.
30、 So if I asked a question, which was ?if I knew the SUID number of one of you in the room, and I asked, 揑s this your SUID number? And you say, 揘o.? And I ask you, 揑s 05179164 your SUID number? Is it yours? Be kind. Tell me it抯 yours. Student: Inaudible. Instructor (Mehran Sahami): It抯 not. So it act
31、ually turns out that it抯 mine, so I would have had to search the entire room before I finally got back to myself if I didn抰 make that shortcut. Okay. So another illustration of what is really a very simple idea ?that if you抮e forced to look at all of the elements that you are searching among, then t
32、hat抯 bad in and of itself if you could do better. So we抣l see if we can do better, but here is a rather larger data set ?286 area codes ?the three-digit numbers that come before the rest of the seven digits in your phone numbers. But we抮e trying to find 650, which is this county抯 area code. So here抯
33、 a larger array. What would happen if we were searching in this? Well, it depends. If we have a fast computer, it may not matter. These are ?there抯 only about 300 numbers here, and doing something 300 times is something that computers are reasonably good at. But I thought I抎 put together this sort of compelling example of why this can become a bad
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1