Quadratic probing


Quadratic probing is an open addressing scheme in computer programming for resolving hash collisions in hash tables. Quadratic probing operates by taking the original hash index and adding successive values of an arbitrary quadratic polynomial until an open slot is found.
An example sequence using quadratic probing is:
Quadratic probing can be a more efficient algorithm in an open addressing table, since it better avoids the clustering problem that can occur with linear probing, although it is not immune. It also provides good memory caching because it preserves some locality of reference; however, linear probing has greater locality and, thus, better cache performance.

Quadratic function

Let h be a hash function that maps an element k to an integer in , where m is the size of the table. Let the ith probe position for a value k be given by the function
where c2 ≠ 0. degrades to a linear probe. For a given hash table, the values of c1 and c2 remain constant.
Examples:
When using quadratic probing, however, there is no guarantee of finding an empty cell once the table becomes more than half full, or even before this if the table size is composite, because collisions must be resolved using half of the table at most.
The inverse of this can be proven as such: Suppose a hash table has size , with an initial location and two alternative locations and .
If these two locations point to the same key space, but, then
As is a prime number, either or must be divisible by.
Since and are different,, and since both variables are greater than zero,. Thus, by contradiction, the first alternative locations after must be unique, and subsequently, an empty space can always be found so long as at most locations are filled.

Alternating signs

If the sign of the offset is alternated, and if the number of buckets is a prime number congruent to 3 modulo 4, then the first offsets will be unique. In other words, a permutation of 0 through is obtained, and, consequently, a free bucket will always be found as long as at least one exists.