9Hashing
What is hashing?
You want to find a student by number or a user by name. Searching an array checks every record one by one; a sorted array or a binary search tree cuts the number of steps but does not get rid of them. Hashing offers another way: compute the place of the record from its key instead of searching for it.
- What direct addressing is and why it usually cannot be used
- How a hash function turns a key into a cell
- Why collisions are unavoidable and how this section deals with them
1Finding a record by key
The core job of many programs is finding the record that belongs to a key: a student from a number, a price from a product code, a meaning from a word. The structures so far do this in these times:
| Structure | Search | Why |
|---|---|---|
| Unsorted array or linked list | O(n) | Every record is checked |
| Sorted array | O(log n) | Binary search drops half at each step |
| Balanced binary search tree | O(log n) | A subtree is dropped at each step |
| Hash table | O(1) on average | The place is computed from the key |
2Direct addressing
If the keys are small numbers, the fastest way is to use the key itself as the index: the student in seat 3 sits in cell 3 of the array. Insertion, search and deletion are a single array access.
The trouble is the range of the keys. Student numbers have nine or ten digits; direct addressing would need an array of billions of cells, while the school has a few thousand students. Almost every cell would stay empty. If the key is text it is even worse: "ali" is not an index.
3Hash functions: from a large key to a small index
Hashing keeps the idea of direct addressing but puts a function in between. The hash function turns a large or text key into a number between 0 and M - 1. The record is written into that cell of an array of M cells, the hash table. The simplest function is the remainder of a division:
#define M 11 int hash(int key) { return key % M; // a cell from 0 to 10}25 % 11 = 3, 40 % 11 = 7, 121 % 11 = 0. However large the key, its place is found with one operation.Search does the same computation: to find 40, compute 40 % 11 = 7 and look at cell 7. However many records the table holds, the work is a few steps.
4Collisions are unavoidable
When you want to add 18 to the table, 18 % 11 = 7: cell 7 already holds 40. There are more possible keys than cells, so two keys taking the same cell cannot be prevented. This is called a collision.
There are two families of answers. Open addressing puts the colliding key into another empty cell inside the table; the table is a plain array. Separate chaining keeps a linked list in every cell; keys that land on the same cell are added to the same list. One is built with an array and the other with a linked list, and this section codes both with the same operations.
5A good hash function
- It is fast. It finishes in a few arithmetic operations; otherwise the gain of hashing is lost.
- It is deterministic. The same key gives the same cell every time; otherwise an inserted record can never be found again.
- It spreads evenly. Keys are spread over the cells as evenly as possible. Collisions stay few and operations stay close to O(1).
The promise of O(1) on average depends on these three. If a bad function piles every key into a few cells, the hash table is no better than a linked list and search falls to O(n).
6What this section covers
- Hash functions: the division method, multiplication, folding and mid-square, string keys, collisions and the load factor.
- Open addressing: inserting with linear probing, search, deletion, quadratic probing, double hashing.
- Separate chaining: insertion, search, deletion and the two methods compared.
- Applications: resizing, counting and finding duplicates, built-in hash tables.
- Exam practice: exam questions and answers.
7Try it
With M = 11, which cell does the key 57 land in?
The answer is A. 57 = 5 · 11 + 2. The remainder is 2, so the key lands in cell 2.
Show the answer
The answer is A. 57 = 5 · 11 + 2. The remainder is 2, so the key lands in cell 2.
Why is direct addressing not suitable for student numbers?
The answer is B. With direct addressing the array is as large as the range of the keys. Nine-digit numbers need billions of cells, and records are written into only a few thousand of them. Hashing shrinks that range to M cells.
Show the answer
The answer is B. With direct addressing the array is as large as the range of the keys. Nine-digit numbers need billions of cells, and records are written into only a few thousand of them. Hashing shrinks that range to M cells.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.