A simple hash table works by storing items in multiple lists, not just one. It uses a very fast and repeatable (i.e. nonrandom) method to select which list each item contains. Therefore, when it is time to find the element again, it repeats this method to find out which list to look at, and then performs a regular (slow) linear search in this list.
By dividing the items into 17 lists, the search will be 17 times faster, which is a good improvement.
Although, of course, this is true only if the lists are approximately the same length, so it is important to choose a good method for distributing elements between lists.
In your example table, the first column is the key, we need to find the element. And let's assume that we will maintain 17 lists. To insert something, we perform an operation on a key called hash. It just turns the key into a number. It does not return a random number, because it should always return the same number for the same key. But at the same time, the numbers should be "widely distributed."
Then we take the resulting number and usage module to reduce it to the size of our list:
Hash(key) % 17
All this happens very quickly. Our lists are in an array, therefore:
_lists[Hash(key % 17)].Add(record);
And then, to find the item using this key:
Record found = _lists[Hash(key % 17)].Find(key);
Please note that each list can only be any type of container or a linked list class that you write manually. When we run Find on this list, it works in a slow way (examine the key of each record).
Daniel Earwicker
source share