 Everyone? So our second search algorithm is the smart sequential search. So it basically works like sequential search, but we can get a little bit of an improvement if our items are sorted. And in fact, smart sequential search only works if the items in the list are sorted. That's okay. Most items can be sorted. You know, strings have an alphabetical order, integers, floats, numeric order, right? But if we have these things sorted ahead of time, we can use this to our advantage. So this might be, for example, a way that you search through library books on the bookshelf. Because, you know, the books on the bookshelf, they have their call numbers on them, the Library of Congress call number. But, you know, that call number is not necessarily like super intuitive. The scale is a little weird of it. But if the library is, you know, if people are doing their jobs at the library and the patrons are courteous, the items will be in order. So when you go into the book, you can say, I'm looking for item qa76.76.b245, right? One way you can find it sequentially is you start like the beginning of the bookshelf and then you just kind of scan upward on these call numbers. But once you know, once you see a call number bigger than the one you're looking for, like I was looking for qa76.76 and I'm going along and I see qa76.76 and then all of a sudden I see qa78. Well, I know that once I see that 78, I've gone too far. My book must not be there. Because if it was there, it would have come before this qa76 book, 78 book, excuse me. That's the idea of a smart sequential search. You're still looking sequentially or linearly up the list. But when you see a value that is greater than your target, well, you know your target must not be in the list. So same thing here. Suppose in this sorted again, the sorting is essential. I've got a sorted list of integers. Suppose I am looking for 45 in the list. Well, sequential search says are you 45? No. 45. No. 45. 45. 45. Are you 45? No, but you're also bigger than 45. So 45 was in this list. I would have found it by now. Well, that's the premise of smart sequential search. So here's the smart sequential search algorithm. It's a little bit, tiny bit different than the sequential search algorithm. It too is on the paper. But let's go through it. Let's trace this again. And this time I'm going to keep track of the number of comparisons that I am doing for each search, right? So let's apply this algorithm by hand. Again, the algorithm is on, I think, the first page of your hand out. Now let's go to the third page. So I'm looking at this example. Here is an ordered list down here near the bottom of the third page. And we're going to search for the same things. We're going to search for 332 for 91. Oh, can't see that. We're going to search for 332 for 91 and for 22. But we're going to use this list. Again, it has to be ordered, sorted for it to work. All right. So let's do it. Let's trace one. Let's start at the top of our algorithm. A is our list of elements in ascending order. Super important, right? List of elements in ascending order. N is the number of elements in A. How many elements are there? There are eight. Okay. Target is the item we want to search. Well, let's start out by searching for target 332. We have a different variable now. We've got done. Done is initialized to false. And location is zero. So again, we start at zero and we count up. Okay. The control loop is pretty similar. While the location is less than n, so while this guy has not gone past the end of the list, and we're not done, whatever that means, we do stuff. So if the target, let's do the first two loops together and then we'll speed it up. Okay. If target 332 is less than a sub look is less than this value. A sub look is the value in the list. Okay. Set done to true. Okay. Well, the target is not less than or equal to this. Okay. Target is not less than or equal to this. So if else set the location equal to location plus one. Okay. So we're effectively moving the location up a slot. Okay. And now we loop back around while location less than n, one is less than eight and not done. Done is still false and not done. Do the following. If the target 332 is less than or equal to a sub look five 332 is not less than five. So we do our else look gets look plus one. Okay. So we shuffle it up. Okay. So let's speed this up a little bit. Right. Is the target less than or equal to six? No. Is the target less than or equal to eight? No. Is the target less than or equal to 91? No. No. No. Okay. Here. Now we're at the end. So when we get to the end the location will have to be seven at this point. We go into our while loop. If target is less than or equal to a sub look. Well, in fact, it is equal to a sub look. Yeah. 333. Our target is equal to this. What do we do? Target is less than or equal to a sub look. We set done to true and we exit the if else block come up here and now this criteria is no longer true. We are done and it is done. So we exit the loop. Okay. Now we've got one final kind of like thing that we're going to do here. If look equals equals n. Okay. Look does not equal and look is still seven. Right. Look is still seven or target does not equal a sub look. Okay. Well, the target is 332 and it does equal a sub look. So we don't do this. We skip over this. Right. We just return look, which is seven. Right. So the smart sequential search is giving us back the index of the item in the list. Okay. How many comparisons did we do? Well, we did one, two, three, four, five, six, seven, eight. We did eight comparisons here. So that's okay. We actually had to look at everything in a list. That's not great. Right. There's no improvement here of the smart sequential search. How about 91? What happens if our target becomes 91 and we reset our loop? Okay. Well, I'm not going to walk through the whole thing again. Let's just do it quickly. What do we do? Is the is the target less than or equal to this guy? No. Less is 91 less than or equal to five. No. No. No. Yes. 91 is less than or equal to 91. Okay. So when look is for we stop, we have found it. Right. How many comparisons did we do? Well, we compared it the target to one to this one, two, three, four, five, five comparisons. Okay. All right. Now, 22. This one's a little more interesting, right? This is where the smartness comes in to the smart sequential search. So let's reset our algorithm here. When I say reset our algorithm, I'm just zeroing out initializing everything over here. Our target now is 22. Okay. Now, here, we're going to have to count the number of comparisons very carefully. How are we counting comparisons? We count a comparison when we compare the target to an element of the list. Okay. So let me do it like Roman tick mark style down here. All right. I'm going to run the algorithm quickly though. Is the target less than or equal to a sub look? Yes. One comparison. And we move the location up. All right. That's what our algorithm says to do. Is the target, or excuse me, is the target less than or equal to a sub look? No. Okay. So we move our location up a slot. Is our target 22 less than or equal to six? No. Whoops. I've forgotten to put my tick marks in here. Five, six. Okay. Next comparison is the target 22 less than or equal to eight. No. But that is a comparison is target less than or equal to eight. Right. Now I'm up here. Oh, I don't know why I said an equal to zero. That's wrong. Okay. Now I'm up here. Right. Let's go back to the beginning loop while location is less than n. It is five is less than eight. And not done. I haven't found it yet. So I'm not done. If target less than or equal to a sub look, target is less than or equal to a sub look. It is less than. Right. That's one final comparison here. So I've got five, five comparisons. Now I jump out of my loop. Right. I come back up to my while loop. But I'm now done. Right. Because the target is less than or equal to this value. Right. So if the target is less than or equal to the value set done equal to true, come back up to my while loop. And look is still less than n. But now I am done. Right. Now I am done. So this fails. I break out here. And now I'm down to my if statement. Okay. If look equal equal and look is not equal to and look is still five and is a or target not equal a sub look. Target is 22. And it does not equal a sub look. Okay. So or this is true. This evaluates to true. Something or true, we go inside our loop set look equal to none looks look equal to none. And finally return the look. Okay. So interesting. Right. Now we know. Right. We've kind of short circuited our loop. We determined because this value is bigger than our target. And we haven't seen our target yet. It can't possibly be in there. So this is the smartness of the smart sequential search. When the item is not in there, you might be able to stop looking sooner. But all that depends on the fact that this thing is sorted. This does not work if the list is not sorted. If the list is unordered. Okay. All right. So let's answer these questions. This question is the same as this one in your worksheet. Right. How do you know when the target is not present? Well, you know that it is not present when the target is less than a sub look. Right. If it's not in there. Right. The target is less than this. It's not there. Or the other case could be. And this one we didn't do an example of. The other case is when you're looking for a target that's so big, it's bigger than the biggest thing. Okay. If the target were, say, 999, 9922, 9,922. You would wind up walking all the way off this list. In which case you would stop. It would be just like sequential search. Right. If you walk off the end of the list, it's definitely not in there. Right. So, or the location is equal to n. The size of the list. Okay. So that's how you know when a target is not present using smart sequential search. So the smart linear search or smart sequential search, again sequential versus linear same thing in this context, compares two objects using less than or equal. Okay. What method must be defined in a Python class, a custom class, like for a bank account or a patient record student, whatever, for it to be comparable with the less than or equal operator? Okay. Well, equal equal, this thing is the EQ method. Less than equal is LTE. Less than equal. And it works just the same as the equal equal method, except, you know, you return true or false from the LTE method. And inside that method, you define what it means to be less than or equal to. So you can use the less than or equal operator in there. Okay. So that's the idea of smart sequential search of a sorted list. All right. Now let's go to our chart. Smart sequential search. All right. So how many comparisons are required when the target is present in the list in the best case? When the best case, where would the element be in the list? In the best case, if it's there, you would find it in the first spot, right? In slot one, just like you did with sequential search. Okay. Same thing. In the worst case, where would it be? Again, it's there. The worst case, well, that's the case we had with 332. In the worst case, you have to compare, do eight comparisons. You have to look at all n items in the list because it's in the last slot. Okay. Same thing. Now let's answer the question. What if the thing is not in the list? Okay. What's the best case if the item is not in the list? Okay. Well, let's scooch up here again. What would be the best case here? Well, the best case would be you're looking for a target that's less than the first element in the list. Say your target would be negative one. Okay. Well, if it's less than the smallest item in the list, right away, your while loop will break out because it'll say a sub-loak is less than, or excuse me, target is less than a sub-loak. I'm done. It's not there. So in the best case for a smart sequential search, you only have to look at one item. Okay. So that's an improvement up here, right, over the sequential search. In the best case, there's only one comparison. In the worst case, what's the worst case? Well, the worst case is the one I mentioned a second ago. The worst case is the thing you're looking for is much bigger than the biggest, or is bigger at all than the biggest thing in the list, in which case you have to go through every single thing and walk off the end of the list to know, yeah, my target's not in there. So in the worst case, I'm still in. All right. Now again, all of this comes with a cost. We do have a better best case scenario, but we have to have a sorted list. Now, if the list sorting is not free, sorting is actually quite expensive. And we will talk about sorting algorithms later in the semester. Sorting has a cost associated with it. And then sometimes you can't sort things, right? Especially if you have a list like Imagine in Python, you can have a list with mixed types. You can have a list that has ints and floats and strings and whatever else in them. You can't sort that list because there's no way of saying a string is less than an integer. It just doesn't make sense. It's literally comparing apples to oranges there, okay? So that's the concept of sequential search. So what do I want you to understand? Understand how sequential search works, right? That's pretty easy, I think. Understand also how the smart sequential search improves upon sequential search and what must be true about the list in order for that to work, okay? Next video, we are going to talk about binary search. Now, binary search is a whole different animal. It is a very clever algorithm. It looks very different. And actually, it does better than Big O event. So it is going to be our first algorithm that performs better than Big O event. We'll talk about that in the next video.