Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/math/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 为什么这种算法适用于二次探测?_Algorithm_Math_Hash_Hashtable_Quadratic Probing - Fatal编程技术网

Algorithm 为什么这种算法适用于二次探测?

Algorithm 为什么这种算法适用于二次探测?,algorithm,math,hash,hashtable,quadratic-probing,Algorithm,Math,Hash,Hashtable,Quadratic Probing,为什么将偏移量增加2是有意义的?我知道这个算法是有效的,我已经将结果绘制成x^2类型的图形,但我就是看不到。有人能简单地解释一下吗?谢谢大家! size_t FindPos(const HashedObj & x) const { size_t offset = 1; size_t current_pos = InternalHash(x); while (array_[current_pos].info_ != EMPT

为什么将偏移量增加2是有意义的?我知道这个算法是有效的,我已经将结果绘制成x^2类型的图形,但我就是看不到。有人能简单地解释一下吗?谢谢大家!

        size_t FindPos(const HashedObj & x) const {
        size_t offset = 1;
        size_t current_pos = InternalHash(x);

        while (array_[current_pos].info_ != EMPTY && array_[current_pos].element_ != x) {
           current_pos += offset;  
           offset += 2;
           if (current_pos >= array_.size())
              current_pos -= array_.size();
           }
       return current_pos;
      }

在每次迭代中,您将
偏移量增加
2
,因此在
n
第次迭代后,if将具有值
1+2*n

在每次迭代中,您都要将其添加到当前位置。因此,在第一次迭代中,您将
1+0*2
添加到
current\u pos
,在第二次迭代中添加
1+1*2
,在第三次迭代中添加
1+2*2
,依此类推

因此,在主体的第次迭代
m
结束时添加到
当前位置的总量将是
1+2*n
n=0
n=m-1
的总和,暂时忽略模运算

使用加法的线性,该和可以写成
m+2*和(n表示n=0到m-1)
。剩余的总和可以显示为
m*(m-1)/2
,因此总数为

m + 2 * m*(m-1)/2 = m**2

因此,此方法与在每次迭代中使用
(当前位置+m**2)%array.size()
相同,
m
作为循环计数器,而不修改
当前位置
将非常感谢!现在它变得更有意义了。