Test your knowledge with free interactive questions on Seneca — used by over 10 million students.

Hash Tables

A hash table is a data structure used for very large collections of data or where quick access to the data is required, such as customer records or password verification.

Hashing algorithm

Hashing algorithm

  • Hashing algorithms allows for the efficient location and retrieval of data.
  • The hashing algorithm generates indexes based on the key fields of sets of data.
  • A common example of a hashing algorithm is one which takes the key and divides it by the number of storage spaces available.
  • The remainder is then used as the address at which to store the key.
Example

Example

  • If the keys 738, 271 and 560 need storing in a hash table with 6 storage spaces, the process would be as follows:
  • Calculate the remainder when 738 is divided by 6.
    • 738 MOD 6 = 0
  • That means first key should be stored in location 0.

Example cont.

  • For the second key, 271 MOD 6 = 1.
    • We store this in location 1.
  • For the third key, 560 MOD 6 = 2.
    • We store this in location 2.
Collisions

Collisions

  • A perfect hashing algorithm will always generate a unique index.
  • Should this not be the case, a ‘collision’ is said to occur .
  • When a collision occurs, a rehashing algorithm is used to determine the next available slot in which to store the data.
  • Alternatively, we can create a separate data structure such as a linked list from each storage location and store the collisions there.
Example

Example

  • Key 362 would need adding to the hash table above in location 2 as:
    • 362 MOD 6 = 2
  • However, it’s already filled by 560.
  • In this case, the next available slot is location 3, so key 362 is stored there.
Jump to other topics
1

Components of a Computer

2

Software & Software Development

3

Exchanging Data

4

Data Types, Data Structures & Algorithms

5

Legal, Moral, Cultural & Ethical Issues

6

Elements of Computational Thinking

6.1

Thinking Abstractly

6.2

Thinking Ahead

6.3

Thinking Procedurally

6.4

Thinking Logically

6.5

Thinking Concurrently

7

Problem Solving & Programming

8

Algorithms

Unlock your full potential with Seneca Premium

  • Unlimited access to 10,000+ open-ended exam questions

  • Mini-mock exams based on your study history

  • Unlock 800+ premium courses & e-books

Get started with Seneca Premium