Data Structures · Hashing
Search as the Dominant Operation on Static Data Sets
Overview
A data structure mainly stores data, either as input to an algorithm or to hold its output. Many real data sets are static, so search becomes the dominant operation; hashing is a design that makes this search as fast as possible, achieving average search time.
Key concepts
- Three standard operations: every data structure supports insert (add an element), search (find a particular element) and delete (remove an element).
- Static data set: a data set that is created once and rarely changes — elements are inserted and then left as they are, without modification.
- Dominant operation: on static data sets, search is the operation performed most often, so data structures are designed with search in mind.
- Hashing: a data structure design whose purpose is to make searching faster, giving average search time.
How it works
- Conventional structures give limited search performance: unsorted array , sorted array (binary search), linked list , binary tree (traversal), normal BST worst case, height-balanced BST , priority queue for min/max but for an arbitrary element.
- The best possible search time on most of these structures is .
- Because search is used so frequently, the goal arose to minimise search time as much as possible.
- The solution is hashing: with hashing, the average time to search an element is , i.e. constant time.
Formulas
- Unsorted array search:
- Sorted array search (binary search):
- Linked list search:
- Binary tree search (traversal):
- Normal BST search, worst case: (height can be )
- Height-balanced BST search: (height is )
- Priority queue, search min or max:
- Priority queue, search an arbitrary element:
- Hashing search, on average:
Comparison
| Data structure | Time to search an element | Reason |
|---|---|---|
| Unsorted array | May compare with all elements | |
| Sorted array | Binary search | |
| Linked list | May scan the entire list | |
| Binary tree | Traversal needed | |
| Binary search tree (normal) | worst case | Tree can become a linked list; height can be |
| Height-balanced BST (AVL, red-black, 2-3 tree) | Height is | |
| Priority queue (min heap / max heap) — search min or max | Min/max directly available | |
| Priority queue (min heap / max heap) — search a particular element | Must examine elements | |
| Hashing | on average | Hashing technique |
Revision summary
- The three standard operations are insert, search and delete.
- A static data set is created once and rarely changes; search is its dominant operation.
- The best search time with conventional structures is (sorted array, balanced BST).
- A normal BST degrades to a linked list in the worst case, giving search; balancing (AVL, red-black) brings it to .
- In a heap, min/max search is but searching an arbitrary element is .
- Hashing gives search on average — the average qualifier must not be dropped.
Previous-year exam insights
- Hashing gives average search time but worst case, because all keys can collide into the same slot; the worst-case bound must not be confused with the average.
- The load factor α = n/m (number of stored keys divided by number of slots) determines performance;
with chaining, unsuccessful search takes Θ(1+α) and successful search Θ(1+α) on average, and open addressing requires α < 1. - A good hash function should be easy to compute and distribute keys uniformly (e.g. division method h(k)=k mod m with m prime, or multiplication method);
a poor hash function causes clustering and degrades search toward . - Collision resolution methods: separate chaining (each slot holds a linked list) and open addressing (linear probing, quadratic probing, double hashing); linear probing suffers primary clustering, and deletion in open addressing needs tombstones/lazy deletion.
- Hashing does not support efficient ordered operations: finding the minimum/maximum, predecessor/successor, or printing keys in sorted order takes (or by sorting), so a balanced BST is preferred when order matters.
- In a min-heap or max-heap, insert and delete-min/delete-max take and building a heap takes ; only the root is directly accessible in .