Hashes
Project Description
This project explores how hashing functions distribute data across fixed-size tables and how different collision resolution strategies affect performance in practice. I implemented and tested several hashing techniques, then compared how each one behaves under different load factors and input patterns.
It also connects theoretical/matheatical ideas like modular arithmetic (for cycles) and probability (when it comes to performance) with more practical design decisions used in real-world data structures such as hash maps.
Hashing Algorithms
- Linear Probing: Handles collisions by checking the next available slot in sequence. It’s simple to implement, but performance can degrade due to clustering when the table becomes full.
- Quadratic Probing: Reduces clustering by using quadratic offsets when searching for an open slot. This improves distribution compared to linear probing, especially at moderate load factors.
- Linked List (Chaining): Stores multiple values at each index using a linked list. This approach stays more stable under higher load factors since collisions don’t block insertion.
- Cuckoo Hashing: Uses two hash functions and resolves collisions by “kicking out” existing elements and relocating them. It achieves very fast lookups in most cases but comes with higher complexity and memory usage. It is named after the Cuckoo bird, who lay their eggs in other birds' nests and "kick out" the orignal bird's eggs.
Summary of Hash Analysis
Performance varied quite a bit across implementations depending on load factor and input distribution. Interestingly, datasets with clustered values (Dataset D) sometimes performed just as well as, or even better than, uniformly distributed ones thanks to how effectively the hash function spread values.
Linear and quadratic probing behaved as expected, with performance dropping as the load factor increased. Chaining remained more stable under heavier loads, while cuckoo hashing consistently produced the fastest lookup times, though with higher memory cost.
Overall, the results highlight the tradeoffs between simplicity, speed, and memory usage when designing hash tables.
Key Learnings
- Good hash function design often matters more than the raw distribution of input data.
- Each collision resolution strategy introduces its own balance of speed, memory usage, and complexity.
- Load factor is one of the strongest indicators of how a hash table will perform under stress.
- Real-world benchmarking doesn’t always match theoretical expectations and can reveal unexpected behavior.
- Cuckoo hashing delivers strong lookup performance, but it requires careful implementation and more memory.