Chapter 4 · Simulation

Hash and Interpolation Search

← Chapter 4

Hash table search h(k) = k mod 8

Chaining: each bucket holds a list of keys.

Interpolation search sorted array

pos = lo + ⌊(x − A[lo]) · (hi − lo) / (A[hi] − A[lo])⌋

    Hash and Interpolation Search

    Compare bucket lookup in a simple hash table with interpolation search on a sorted array of numbers.

    How to use

    • Hash table: enter a key and press Find. The bucket h(k) = k mod 8 is highlighted and its chain is scanned from left to right. Insert appends a new key to the end of its chain (to create collisions); Reset restores the initial table.
    • Interpolation search: enter a target; press Search to animate the probes or Step to advance one probe at a time. The shaded cells are the current range [lo, hi]. New array generates a new sorted array (sometimes uniformly distributed, sometimes skewed).

    Searching with a hash

    The hash function maps a key directly to a bucket. With a good hash function and load factor α = n / m, a search in a chained table takes O(1 + α) time on average; in the worst case (all keys in one bucket) it takes O(n).

    Interpolation search

    Instead of always probing the middle, it estimates the position of x from the values at the ends of the range:

    pos = lo + ⌊(x − A[lo]) · (hi − lo) / (A[hi] − A[lo])⌋

    On uniformly distributed sorted data it needs O(log log n) probes on average; on skewed data it can degrade to O(n). Binary search, by contrast, always needs O(log n) probes.