Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 在插值搜索中计算mid?_Algorithm_Search - Fatal编程技术网

Algorithm 在插值搜索中计算mid?

Algorithm 在插值搜索中计算mid?,algorithm,search,Algorithm,Search,我知道,插值搜索是对二进制搜索的一种改进,在二进制搜索中,输入在每次迭代中通过计算分成两等分 mid = (low + high) / 2 在插值搜索中,mid计算为 mid = low + (key - arr[low]) * ((high - low) / (arr[high] - arr[low])) 现在我需要理解插值搜索中计算mid的公式 Ref:您可以将数组arr视为一个函数f,它作用于索引并返回一个单调的值(因为数组是排序的)。所以你有你的初始数据f(低)=m和f(高)=m。现

我知道,插值搜索是对二进制搜索的一种改进,在二进制搜索中,输入在每次迭代中通过计算分成两等分

mid = (low + high) / 2
在插值搜索中,mid计算为

mid = low + (key - arr[low]) * ((high - low) / (arr[high] - arr[low]))
现在我需要理解插值搜索中计算
mid
的公式


Ref:

您可以将数组
arr
视为一个函数
f
,它作用于索引并返回一个单调的值(因为数组是排序的)。所以你有你的初始数据
f(低)=m
f(高)=m
。现在你可以用一条直线来插值你的函数
f
,这是非常合理的,因为你的
f
是单调的,你只有2个点。 因此,插值应该是通过抛出点
(低,m)
(高,m)
的直线(线性函数)。这是它的方程式

(y - f(low))/(f(high) - f(low)) = (x - low)/(high - low)
因此
y
这里是搜索空间的元素,
x
来自域(x是数组的索引)。因此,如果函数
f
与其插值相同,则
键的索引将为:

x = low + (high - low)*(key - f(low))/(f(high) - f(low))
因此,假设你的函数
f
接近它的插值,你应该检查
x
处的值
f
,看看它是否是目标。否则,只需缩短插值间隔

只是另一个奇怪答案的延伸

他提出的方程式是基于(直线方程)

现在,将
f()
视为获取索引并返回其
y
轴值的函数

y1 = f(low)
y2 = f(high)
x1 = low
x2 = high

(y - f(low)) = [(f(high) - f(low)) / (high - low)] * (x - low);

OR

x = low + [(high - low) * (y - f(low))] / (f(high) - f(low))

here: y = key
we are looking for position(value) of x.

假设
low=10
high=20
arr[low]==100
arr[high]==200
。现在计算
key==110
key==150
、和
key==190
@Henrik的
mid
,但这个公式是如何推导出来的?如何得到这个公式-