Hashing Hash Function Keys Hash Table/Array Collisions Chaining/Linear Probbing
<aside> đź’ˇ Hashing involves using a hash function to map keys to a certain location in a hash table. ****Hash tables are similar to dictionaries and can be thought of as implementing a dictionary using a hash function.
</aside>
<aside> 💡 One example of a hash function is modulo where given a hash table of a certain size and a number $x$, $x$ will be modded by another number. The result will determine $x$’s placement in the hash table.
</aside>
<aside> đź’ˇ Given a hash table of size 4 and a hash function that takes in a key, $k$, and mods it by 4, draw the hash table when inserting the keys in the following order: 20, 35, 41, 62, 120.
Hashing Steps:
</aside>

<aside> 💡 When running our hash function on 120, we encounter that 120 also needs to be stored in the 0 location of the hash table. When multiple keys are stored in the same location, this is known as a collision. When encountering collisions, chaining (also known as linear probbing) is applied where a linked list is created with the keys in the same location, serving as a “bucket” of values.
</aside>
| Operation | Hash Table (w/ chaining) |
|---|---|
insert() |
$O(n)$ |
search() |
$O(n)$ |
<aside> 💡 Of course, the best case for hashing would be $O(1)$ for insertion and searching if there was no chaining. However, taking chaining into consideration, insertion and searching a hash table would take $O(n)$ respectively. You’ll have to iterate through each and every single key in a certain location to find a desired key. For insertion, you’d have to go through each and every single key to determine if the desired key has already been inserted beforehand.
</aside>