What is Search Algorithm Definition
this is a best method but just for the purposes of demonstration in this episode we're just going to stick with that so what I'm going to do in this particular case is every time there is a hash collision I'm just going to keep going down in hash table until I find an empty slot so if we have cat in this position and I want to insert camera which is mapped to the same position what I'm going to do is I'm going to move down position fall is empty so I'm going to insert the word camera then alright that's a problem solved but wait that just creates another problem if I'm inside the wet dock right now it's going to try to go to position four and it's going to say hey that's a camera in that place so it's going to have to move down and it's going to occupy slot number five which is identity is the slot for any word starting with the letter e but once again I don't go too deep into the specifics behind actual hashing I'm more interested in this searching aspect so now what happens if I want to search for camera I'm going to go to the hash function hash function it's going to tell me position three I'm going to go to position three and say well that doesn't look like a camera but this is not enough to tell me that what camera is not phone what we're gonna have to do is we're going to have to apply the same logic as set of hash collision we're gonna have to keep going down to see if perhaps during insertion collisions have a cut to make it you know difficult to find I move down by one slot and I realize hey that's where it is what happens when you search for an item that is missing let's try to search for conflicts the hash function points you in a position free and you say nope that is not what I'm looking for you try to go down the position for that's not what I'm looking for either you go around position five nope that's on it you go down to position six and you say hey that's an empty slot only then can you conclude that the item it's not found all right so now that we've looked at an example let us now come back and try to consolidate what we've seen so far firstly yes searching with a hash table can be very first depending on a plethora of different factors like how well the hash function is written what size
is the hash table and what kind of input data do you normally get in the average case when you try to search for something it should be found very quickly however if conditions aren't favorable what's going to happen is things can start getting slow when there are a lot of collisions and you have to keep doing collision detection logic what's going to happen is every time you perform as such you're gonna have to go through multiple elements in the table before we can come to your conclusion in fact the worst case comes about when the entire table is filled when you perform a search in the worst case what's gonna happen is well it's just gonna go through everything in the entire table what this means is unfortunately the worst case time complexity for hash algorithm is all N how the fingers crossed this doesn't happen very often and as a result I want to emphasize that hashing has the average case time complexity of or one that is constant time now do bear in mind that the or one time complexity doesn't mean that I can find things in one step it just means that I can find something in a constant number of steps that is number of steps not bound or determined by the number of items in the list so despite its shortcomings hashing is actually used very often many practical implementations of hashing actually allow for say a hash table to expand when needed hash functions can also be dynamically generated - actually you know what best for the input data that's there and what happens is you get optimal performance hashing is also a great way of mapping one data type to another but that is not a feature that we are really going to discuss in depth today and that is it that is pretty much all I have for this particular episode as well as this entire series I know I've only covered three different types of search algorithms and most of them have pretty serious shortcomings but hey that's how it is now if we want to perform even more different types of complicated searches we might have to look at more complicated structures like you know graphs things like that which I'm not very comfortable with covering yet so as a result if that happens there'll be a thing for the future hopefully what I've covered so far is at least a primer for you to understand some of the more basic ways of performing searches...



No comments:
Post a Comment