Hash function
k is the integer key; m is the current table capacity. This normalization keeps the bucket index between 0 and m − 1, even when k is negative. With separate chaining, place the key in bucket h(k).
Explore how a hash function maps keys into a finite table, how collisions are resolved, and why performance changes as the load factor rises. Compare separate chaining with linear probing, then watch the table grow and rehash when it crosses its load threshold.
Step inserts one key and reports its probes. Reset returns to the empty table using the entered key sequence.
Input sequence
The table begins with seven empty buckets.
k is the integer key; m is the current table capacity. This normalization keeps the bucket index between 0 and m − 1, even when k is negative. With separate chaining, place the key in bucket h(k).
Start with probe count i = 0. If that slot is occupied, increment i and check again, up to m positions. The first open slot receives k. If every slot is full, the table must grow before insertion.
For k = 16, h(16) = 2. If bucket 2 already contains 9, separate chaining adds 16 to that bucket’s chain.
p(16, 0) = 2 → occupied
p(16, 1) = 3 → next slot
If slot 3 is empty, insert 16 there after two probes.
For chaining, α = number of keys ÷ buckets and may exceed 1. For open addressing, each slot holds at most one key, so keeping α below 1 is essential. As α rises, linear probing tends to inspect longer clusters.
When α exceeds the selected threshold, the table grows to the next prime at least twice as large and all keys are rehashed.