algoritmauzmanı
Sign in
TREN
9Hashing
Lesson 9.0

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. offers another way: compute the place of the record from its instead of searching for it.

What you will learn
  • What direct addressing is and why it usually cannot be used
  • How a turns a key into a cell
  • Why are unavoidable and how this section deals with them
6 min

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:

StructureSearchWhy
Unsorted array or linked listEvery record is checked
Sorted arrayBinary search drops half at each step
Balanced binary search treeO(log n)A subtree is dropped at each step
Hash table on averageThe 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.

0Ali12Ece34Can567
Direct addressing: the students in seats 1, 3 and 5 are in cells 1, 3 and 5 of the array.

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 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 . 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}
1210122534564078910
An 11-cell table: 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 .

12101225345640740 and 188910
40 and 18 land on the same cell. The whole design of a hash table is the answer to what to do in this case.

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 .

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 .

6What this section covers

7Try it

With M = 11, which cell does the key 57 land in?

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?

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.