Transition from linear sounding to quadratic sounding (hashing)

My current hash table implementation uses Linear Probing, and now I want to move on to Quadratic Probing (and later to chaining and possibly double hashing as well). I read several articles, textbooks, Wikipedia, etc. But I still don’t know exactly what I should do.

Linear sounding basically has step 1 and is easy to do. When searching, inserting or deleting an item from the hash table, I need to calculate the hash, and for this I do this:

index = hash_function(key) % table_size;

Then, when searching, pasting or deleting, I go through the table until I find a free bucket, for example:

do {
    if(/* CHECK IF IT THE ELEMENT WE WANT */) {
        // FOUND ELEMENT

        return;
    } else {
        index = (index + 1) % table_size;
    }
while(/* LOOP UNTIL IT NECESSARY */);

, , , , , , . , .

, , - , ( ). - - ?

EDIT: , , , . http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_hashtable.aspx:

15   for ( step = 1; table->table[h] != EMPTY; step++ ) {
16     if ( compare ( key, table->table[h] ) == 0 )
17       return 1;
18 
19     /* Move forward by quadratically, wrap if necessary */
20     h = ( h + ( step * step - step ) / 2 ) % table->size;
21   }

2 , ... , c(i)=i^2. - c(i)=(i^2-i)/2

, :

index = (index + (index^index)) % table_size;

... :

index = (index + (index^index - index)/2) % table_size;

, :

index = (index + (index^index)/2) % table_size;

... , . , ...

1) ?
2) 2?

+3
2

- . 1, 2, 3.

. . , c(i) = i^2:

alt text

:

alt text

.

, , . :

, ; , , , . , , .


: , . , i^2, . h(k,i) = (h(k) + i + i^2)(mod m).

"". "" - ? ? ?

, - , . - , , , , .

+4

, 2:

step = 1;

do {
    if(/* CHECK IF IT THE ELEMENT WE WANT */) {
        // FOUND ELEMENT

        return;
    } else {
        index = (index + step) % table_size;
        step++;
    }
} while(/* LOOP UNTIL IT NECESSARY */);

, 0, 1, 2, 3, 4... , 0, 1, 3, 6, 10... (i th (i * (i + 1))/2, .. ).

- ( , ) , 2.


:

  • n, , n (i * (i + 1))/2 (mod n) = 0... n-1.
  • . , n : , [0, n-1], (i * (i + 1))/2 (mod n ) . p q, p < .
  • . (p * (p + 1))/2 = (q * (q + 1))/2 (mod n)
  • = > (p 2 + p)/2 = (q 2 + q)/2 (mod n)
  • = > p 2 + p = q 2 + q (mod 2n)
  • = > q 2 - p 2 + q - p = 0 (mod 2n)
  • Factorise = > (q - p) (p + q + 1) = 0 (mod 2n)
  • (q - p) = 0 - p = q.
  • (p + q + 1) = 0 (mod 2n) : p q [0, n-1] q > p, (p + q + 1) [2, 2n-2].
  • 2n, , , 0 (mod 2n):
    • , (q - p) (p + q + 1) (2p + 1), , , .
    • (q - p) (p + q + 1) = 0 (mod 2n) = > (q - p) (p + q + 1) 2n. n (, , 2n) 2, , 2n ( 2n 2, ).
    • (q - p) n-1, (p + q + 1) 2n-2 ( 9), 2n.
    • .
  • , , n ( 2), .

( 2, 10 .)

+11

Source: https://habr.com/ru/post/1734693/


All Articles