Hash table, also called hash table. Features: Find fast.
The gist: Hash table implementation, collisions, and hash functions.
5.1 Hash functions
Require:
- The input and output of the hash function must be consistent, that is, a fixed input must correspond to a fixed output value;
- The input and output of the hash function are best unique, that is, ideally the mapping is unique.
def check_voter(name): if voted.get(name): print ("kick them out") else: voted[name] = True print ("let them vote!")
The code above is just a dictionary to illustrate the usage of the hash table.
5.2 Application
Hash tables are often used for caching, DNS resolution, etc.
5.3 Conflicts
If two keys map to the same location, it is solved by storing a linked list.
5.4 Performance
The hash table is O(1) on average and O(n) worst case.
A good hash function will always have a fill factor less than 1.
Summarize:
- Hash tables can be created by combining hash functions and arrays
- A hash function that minimizes collisions should be used
- Hash table lookups, insertions and deletions are very fast
- Hash table suitable for simulating mappings
- Once the fill factor exceeds 0.7, it is time to adjust the length of the hash table
- Hash tables can be used to cache data
- Hash tables are great for preventing duplication
Example: Hash table, one of the data structures implemented in C language
https://blog.csdn.net/smstong/article/details/51145786