Following Linear and Quadratic Probe Sequences

When two keys are assigned the same initial slot, a hash table needs a consistent way to find another location. Linear and quadratic probing resolve this collision by checking other slots in the same array. You use these methods both to place a new key and to retrace where an existing key could have been placed.

Generate the indices

A probe sequence is the ordered list of table indices inspected for a key. Let mm be the number of slots, indexed from 00 to m−1m-1, and let h(k)h(k) be the key's initial, or home, index. The attempt counter ii begins at zero, so the home slot is the first inspection.


The two rules are:


Linear:pi(k)=(h(k)+i) mod m,Quadratic:pi(k)=(h(k)+i2) mod m.\begin{aligned} \text{Linear:}\qquad p_i(k)&=(h(k)+i)\bmod m,\\ \text{Quadratic:}\qquad p_i(k)&=(h(k)+i^2)\bmod m. \end{aligned}


Here, modulo keeps every index within the array. Linear probing uses offsets 0,1,2,3,…0,1,2,3,\ldots, wrapping from the last slot back to zero. The displayed quadratic rule uses offsets 0,1,4,9,…0,1,4,9,\ldots. Other quadratic rules use c1i+c2i2c_1i+c_2i^2; always use the coefficients supplied by the problem.


Two rows show the first five indices checked by linear and quadratic probing.


Calculate each offset from the original home index. For quadratic probing, do not add the next square to the previously inspected index. The differences between successive square offsets are 1,3,5,…1,3,5,\ldots, rather than the squares themselves.

Insert and search consistently

For insertion of a new, distinct key, inspect the sequence in order and store the key in the first empty slot. An occupied slot containing a different key requires another inspection. Count the initial home-slot inspection and the successful empty-slot inspection when measuring the work.


For lookup, regenerate exactly the sequence used during insertion. Finding the key means success. Assuming no entries have been removed, reaching an empty slot means the key is absent: insertion would have placed it there before reaching any later position.



Compare clustering and coverage

Primary clustering is the growth of consecutive occupied runs that attract further insertions under linear probing. A key whose home index lies anywhere within such a run may scan to its end. As runs grow or merge, insertions and lookups can require more inspections.


Feature

Linear probing

Quadratic probing using square offsets

Next locations

Consecutive slots, with wraparound

Slots at squared offsets from home

Adjacent occupied runs

Can grow through primary clustering

The sequence jumps beyond adjacent slots

Keys sharing a home index

Follow identical sequences

Follow identical sequences

Coverage of all slots

Visits all mm slots in mm attempts

May visit only a subset of slots


Quadratic probing reduces the tendency to scan through one adjacent run, but it does not eliminate every kind of overlap. Keys with the same home index still follow the same sequence, producing secondary clustering. Different home indices can also produce sequences that overlap at some slots.


Coverage depends on the table size and quadratic rule. With an odd prime mm, the square-offset rule visits (m+1)/2(m+1)/2 distinct slots before its first repetition. Therefore, fewer than half the slots occupied is sufficient to guarantee an available slot in that sequence. This is a sufficient condition, not a claim that insertion always fails above half capacity.



For either displayed rule, checking i=0,…,m−1i=0,\ldots,m-1 covers every index the sequence can ever reach, because offsets repeat modulo mm after that range. An implementation therefore needs a bounded search rather than an unlimited loop.

What to remember

  • Start at i=0i=0 and include the home slot.
  • Apply every offset to the home index, then reduce modulo mm.
  • Insert and search using the same sequence.
  • Linear probing covers the array; quadratic coverage needs separate justification.

Example: Resolving a Run of Job-ID Collisions

Example: A scheduler stores job IDs in a table with 1313 slots indexed from 00 to 1212. Starting with an empty table, insert IDs 17,30,43,5617,30,43,56 in that order using h(k)=k mod 13h(k)=k\bmod13. The IDs are distinct, and no entries are removed.


The probe count is the number of slots inspected, including the initial slot and the final slot where insertion or lookup stops. For both methods, reset ii to zero for each key.

a) Insert using linear probing

Use pi=(h(k)+i) mod 13p_i=(h(k)+i)\bmod13. Find the final position of each ID and the total number of inspections.

  1. Calculate the home indices. The home index for every key is ________, because 17=13+417=13+4, 30=26+430=26+4, 43=39+443=39+4 and 56=52+456=52+4.
  2. Insert each ID into the first empty slot in its sequence. ID 1717 goes into slot 44 after one inspection. ID 3030 checks 4,54,5 and goes into slot 55 after two inspections.
  3. Continue with the updated table. For ID 4343, checking occupied slots 44 and 55 accounts for ________ before empty slot 66 is checked. It therefore takes three inspections. ID 5656 checks 4,5,6,74,5,6,7, taking four inspections.

The placements are 17→417\to4, 30→530\to5, 43→643\to6 and 56→756\to7. The total is


1+2+3+4=10 inspections.1+2+3+4=10\text{ inspections}.


Answer: The IDs occupy slots 4,5,6,74,5,6,7, respectively, after 1010 total inspections.

b) Insert using quadratic probing

Start again with an empty table. Use pi=(h(k)+i2) mod 13p_i=(h(k)+i^2)\bmod13. Find the final position of each ID and the total number of inspections.

  1. Use the same home index, 44, but calculate squared offsets. At i=2i=2, the offset is ________; at i=3i=3, it is ________. These offsets are measured from the home index, not the previous slot.
  2. Generate the needed indices:


p0=(4+0) mod 13=4,p1=(4+1) mod 13=5,p2=(4+4) mod 13=8,p3=(4+9) mod 13=0.\begin{aligned} p_0&=(4+0)\bmod13=4,\\ p_1&=(4+1)\bmod13=5,\\ p_2&=(4+4)\bmod13=8,\\ p_3&=(4+9)\bmod13=0. \end{aligned}


  1. Insert in the stated order. ID 1717 takes slot 44 in one inspection. ID 3030 checks 4,54,5 and takes slot 55 in two. ID 4343 checks 4,5,84,5,8 and takes slot 88 in three. ID 5656 checks 4,5,8,04,5,8,0 and takes slot 00 in four.

The total is again 1+2+3+4=101+2+3+4=10 inspections. Although the occupied positions differ from linear probing, the insertion counts are equal for these four keys because every key shares the same home index.


Answer: The IDs occupy slots 4,5,8,04,5,8,0, respectively, after 1010 total inspections.

c) Search for an absent job ID

Using the completed quadratic table from part b), search for ID 6969. Find the stopping index and the number of inspections.

  1. Calculate the search's home index. Since 69=65+469=65+4, its home index is ________. Regenerate the quadratic sequence used for insertion.
  2. Inspect slots 4,5,8,04,5,8,0. They contain 17,30,43,5617,30,43,56, respectively, so none matches 6969. The next attempt is i=4i=4:


p4=(4+42) mod 13=20 mod 13=7.p_4=(4+4^2)\bmod13=20\bmod13=7.


  1. Slot 77 is empty. Because no entries have been removed, insertion of 6969 would have stopped there before using any later slot. Its emptiness therefore establishes that 6969 is absent.

Count the empty-slot inspection too. The search checks 4,5,8,0,74,5,8,0,7, giving five inspections.


Answer: The unsuccessful search stops at slot 77 after 55 inspections.

A scheduler uses linear probing, checking consecutive slots with wraparound after a collision. Its table has eight slots and uses h(k)=k mod 8h(k)=k\bmod 8. The current contents are shown below. Where will the new job ID 3030 be inserted?


Index

Content

0

Empty

1

Empty

2

10

3

Empty

4

Empty

5

Empty

6

14

7

23

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A student is reviewing collision patterns in an open-addressed hash table. Match each term to the description that identifies it.

Match each term
A.

Growth and merging of contiguous occupied runs as unit-step probing inserts keys with different starting indices.

B.

The ordered list of table indices inspected when inserting or searching for a key.

C.

Keys sharing a starting index follow identical quadratic offsets and compete along the same route.

Primary clustering

Probe sequence

Secondary clustering

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A cache uses quadratic probing with pi=(14+i2) mod 17p_i=(14+i^2)\bmod 17 in a table indexed from 00 to 1616. Complete the probe sequence for attempts i=1i=1 through i=5i=5. Enter each index as an exact integer; the initial attempt is supplied.

Attempt iProbe index
014
1
2
3
4
5

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice