Please enable JavaScript.
Coggle requires JavaScript to display documents.
Grokking algorithms 2 (Chapter 5: Hash Tables (hash functions (A hash…
Grokking algorithms 2
Chapter 5: Hash Tables
hash functions
-
Put a hash function and an array together,
and you get a data structure called a hash table.
And hash tables use an array to store the data, so they’re
equally fast.
-
Collisions
There are many different ways to deal with
collisions. The simplest one is this: if multiple keys map to the same slot, start a linked list at that slot.
-
two lessons
• Your hash function is really important. Your hash function mapped all the keys to a single slot. Ideally, your hash function would map keys evenly all over the hash.
• If those linked lists get long, it slows down your hash table a lot. But they won’t get long if you use a good hash function!
Performance
To avoid collisions, you need
• A low load factor
The load factor of a hash table
is easy to calculate.:

A good rule of thumb is, resize when your load factor is greater than 0.7.
• A good hash function
-
-
If you’re really curious, look up the SHA function (there’s a short description of it in the last chapter). You could use that as your hash function.
note:
this talk about how to implement a hash table, but you’ll never have to do that yourself. Whatever programming language you use will have an implementation of hash tables built in.
RECAP
You’ll almost never have to implement a hash table yourself. The programming language you use should provide an implementation for you.
-
-
-