 So we've already started to learn about something we called a binary search The idea is that if I had some list in our case, we'll use an array just to kind of view this List If I wanted to do a lookup of nine, you know, where is nine inside of my array I Would have to start right here and go. Oh, it's going. It's at the one. No, it's at the two No, it's at the three. It's no at four five six seven eight I'd finally be able to say that the index was eight and we referred to the fact that well if it was a Did a look up ten or one hundred It's not in here. So My worst-case scenario results in me having to do a lookup of in log or sorry In I have to go through the entire list So we introduced something Referring to a binary search a binary search said that I have some bottom I have some top and what I do is I calculate the middle point in between them in this case It would be here at the five where I would just say since that's an eight. That's a zero Top plus bot top plus bot Equals eight divided by two equals four zero one two three four and so what I do is let's say I'm doing a lookup on My nine in this case Well, I say is my mid which is five Equal to my lookup my my key no So all right. Well, then we make the adjustments that Bob that that mid in that case is too small So we would say it becomes the new bot and I recalculate out my Top plus bot again this time. It's eight plus four, which is 12 divided by two divided by two Six so five six now. I'm at seven. This is now my mid Once again, we do the same thing Mid is still too small compared to key. So we make another change. This becomes my bot This becomes my mid You can start to see it slowly moving moving More and more and more I would continue to adjust until eventually this Got mid the reason why this approach is Kind of often looked at as nicer is because just as we were saying this this was what we would classify as a linear lookup Again, it means I have to go through the entire thing this Binary search we actually would call a bi-sectional search Search there. We are The reason why is because we're actually cutting it in half every single time and so while I didn't magically Use eight times. I use let's see one two three four. So I used actually half of the amount Since I'm cutting it down. This gets classified as a log of N The issue might be though that if we thought about this Implementation for a moment very similar to how we look at things in real life. Let's say for example I have an old old-fashioned Book I know ancient here going on here. So here's my terrible rendition of a book I don't let's not use those lines. I think that's good enough So if I'm doing say a dictionary or a real-world dictionary or a real-world phone book very old-timey things They had some order to them. There was some particular order to them the issue is when I Naturally start to look things up. I don't know Quickly what the middle point is I don't know. Oh put it right here. I'm gonna try I'm going to do my best to get here But in reality, I'm not getting perfect middle and then I have to fiddle around with this and so One of the things we do is we actually sort of try to mirror Human search through something called an interpolation search The idea is we maybe organize things in a manner that is a little bit more Similar to how a person would see things, you know in my case We have a lot more ease. So ease maybe take up this entire section of whatever my list is So I don't need to go through and kind of constantly fiddle through the ease. Maybe I see Here's my starting point. I can jump all the way to like here to get to my D so How we kind of implement this and why this is important is we have something we would classify as a skip list the entire Principle behind this is we can take that binary search implementation that we had up here we can change it into an interpolation search and We can do it through a linked List we can use those data structures that we've learned in the past as a way to Actually kind of implement these things a little bit so one of the things with a skip list is it operates on a a tier system on a Sort of level system and the idea behind that is that at some bottom level We're going to call that s zero skip zero that bottom level is going to have Every element that we happen to have in our list. Let's say I had one two three 40 50 99 in there So all of those elements are going to be stored on this bottom list one of the things we also do to sort of help Out this skip list approach is we also are going to include a negative infinity and A positive infinity this is going to help for searching Inside of our skip list so once again everything here at the bottom is going to have those elements So here on the far left. We've got our negative infinity right beside it We happen to have our one right after that we happen to have our two After that we happen to have our three our 40 our 50 and Then our 99 Followed by our positive Infinity so why a skip list starts to change is I don't just have one List going on here because if we think about it, that's this is an array This is a skip. This is a link list that I have done nothing No, one of the things I do is I actually add additional levels That's why I kind of introduced that in the past I could come in and add a second level one of the things that I have to put in is the Infinities those are going to be super important again for searching. They're going to actually be in every level But now I arbitrarily decide which one of these elements is going to be more important Let's say for example since you see that we happen to have a lot of smaller numbers going on here of one two and three One is going to appear more often Same thing with 40 Why just cuz you know if 40 gets to 50 pretty quickly and then we'll do 99 for the sake of Just posterity Now there's some connection going on here as well. See these all I'm using these Lines to indicate what's next and what's we'll call it below so Below Skip ahead because drawing lines can be very meticulous as you can see The reason why I'm doing this is to kind of indicate now that I have some connection Going on between these nodes remember this is still implemented through a node system through a linked list system I'm going to do one two more levels two more levels. So I come in. I'll use a different color here I'll use blue to represent my second level again. I have my negative infinity Get those lines in there. We have to have them for those as well I'm going to put the one in because again that really does seem like it's going to be a very important. I Make my connections. I'm going to skip all the way to 99. I 40 doesn't seem to be very popular and positive infinity Now the last thing I need to do is I need to make one final list in this case. I'll call it s3 The only things in this list are my negative and positive infinities And as we keep on saying this is to allow for easy lookup So now what I've done is I've created this sort of structure. This is important because as I was saying earlier we're looking at this idea of an interpolation search how people Sort of look through things a little easier I have a happen to have a lot of elements going on that are sort of in the single digit So for our sake, you know, I need a stop a starting point And I don't want to have to go through all these small numbers if I'm very large So I can actually skip really far ahead. So in this case, let's say I wanted to I'm going to scroll down just a hair And I wanted to do a lookup Let's see. I will use yellow for this. I wanted to do a lookup on Key three remember these nodes they can have keys. They're not just elements anymore They it can be anything that we've had in the past So how do I implement this? How do I implement out the or how do I do say for example my my search? So the first thing I'm going to do is I'm going to start right here. I'm going to start at my top level top level furthest For this left and As you can imagine that's negative infinity So I should never just magically be able to go down or you know this if this happens to be what I was looking for awesome done The first thing I do is like I said, I'm starting here. I do a check Is this what I was looking for? Is this what I was looking for? No, it's negative infinity. So I look at my dot next. What is my dot? next Well, since we notice those horizontal lines are telling me that Is my dot next and the question I would ask here So I I first did a lookup does three equal negative infinity. No So I looked to my right. What's my dot next? So three sorry My negative infinity pointed to my positive infinity. And so I say is it Equal to positive infinity? No is it Less than positive infinity so I ask those three questions am I at my element? Am I is my next element the one I was looking for or am I smaller than my element? This actually sort of gets repeated over and over again because while this happens, you know, obviously here Since three is less than negative positive infinity we hit a yes as a result Since I saw a yes I go down a level go down a Let me make that a little bigger down a level go down a Level and so I'm actually going to repeat the exact same process this time off of a New number. So once again, I ask my question. Am I at the same spot? Three equal to negative positive negative infinity. No, I actually can skip that step because we've already done that lookup before But for our sake, I'm just kind of repeating myself now is three equal to whatever my next is My next being one is three equal to one. No now is three Less than one Since we hit a no here. This is super important since we have gone through. It's not equal to it It's not equal to the next it's actually less it our three is larger than our one. We're going to move To the right Our now our comparison in this case is now going to become our one point I don't need to do negative ones anymore So the same approach actually is going to get repeated. I Take this we've already done our lookup of we know it's not that so I then say well, what about my next in This case 99 is and I'm going to scroll down just a hair is three Less than 99 Well just like before if I see a yes, I go down a level. I Saw a yes. I go Down a level as you can imagine. I'm going to do just to kind of jump through it. I see that I'm here Is my dot next equal to three? No is three less than 40 Yes, so what do I do? I go down a level So I've just sort of traversed through this little approach here. Once again. I say is my three Equal to one. No, is it less than two? No Remember that means that that means I can come over one more is three equal to three. Yes, it is and I've just gone through my skipped list