Speed:
Hash Table
Average: O(1)Worst: O(n)Space: O(n)
🗂️How Hash Tables Work
A Hash Table is a data structure that implements an associative array, mapping keys to values using a hash function. It provides fast insertion, deletion, and lookup operations by computing an index into an array of buckets.
The key components are:
- Hash Function: Maps keys to array indices (e.g., key % table_size)
- Collision Resolution: Handles when multiple keys hash to the same index
- Chaining: Uses linked lists to store multiple elements at same index
- Load Factor: Ratio of elements to table size, affects performance
💻Implementation
📊Performance Analysis
Time Complexity
- • Average Insert: O(1)
- • Average Search: O(1)
- • Average Delete: O(1)
- • Worst case: O(n) for all operations
Key Properties
- • Space: O(n) for table storage
- • Load factor: α = n/m should be < 0.75
- • Hash function: Determines distribution quality
- • Dynamic resizing: Maintains performance
🔗 Collision Resolution
- • Chaining: Linked lists at each index
- • Linear Probing: Check next available slot
- • Quadratic Probing: Check slots at quadratic intervals
- • Double Hashing: Use second hash function
🎯 Hash Functions
- • Division: h(k) = k mod m
- • Multiplication: h(k) = ⌊m(kA mod 1)⌋
- • Universal hashing: Randomized functions
- • Cryptographic: MD5, SHA for security