Pular para o conteúdo

Lesson 82: Hash table

English

Calculate where it goes

  • A hashing algorithm turns a record key into a storage location. Here: key MOD 7 gives a slot from 0 to 6.
  • To find a record, calculate the same hash: no need to search the whole table.
  • A collision happens when two keys get the same slot. This lesson stores the second key in the next free slot (and wraps round after the last one).
Key key MOD 7 Slot
1030 1 1
1050 0 0
1037 1 2 (collision)
1044 1 3 (collision)

A hash table with slots 0 to 6: 1050 in slot 0, 1030 in slot 1, 1037 in slot 2 and 1044 in slot 3. 1037 MOD 7 is 1, but slot 1 is taken, so 1037 goes to the next free slot, 2; 1044 also gives 1 and goes to slot 3

Entrar ou criar conta

IGCSE, A-Level & AP