本文共 3226 字,大约阅读时间需要 10 分钟。
线性插值算法实现图像
Problem:
问题:
We are given an array arr[] with n elements and an element x to be searched amongst the elements of the array.
给定一个数组arr [],其中包含n个元素和一个要在该数组的元素中搜索的元素x 。
Solution:
解:
Here, we will be performing Interpolation Search to find the element in the array.
在这里,我们将执行插值搜索以找到数组中的元素。
Interpolation Search is a much better choice of search algorithm than Linear and Binary Search. We can perform a linear search on a very small dataset, a binary search on a large dataset but what if we are given a dataset where we have millions of data rows and columns. Here, the interpolation search comes to the rescue.
插值搜索是比线性和二进制搜索更好的搜索算法选择。 我们可以对非常小的数据集执行线性搜索,对大型数据集执行二进制搜索,但是如果给定的数据集包含数百万个数据行和列,该怎么办。 在这里,插值搜索可以解决。
One of the basic assumptions of this algorithm is similar to that of the Binary Search Algorithm, i.e. the list of elements must be sorted and uniformly distributed.
It has a huge advantage of time complexity over binary search as here, the complexity is O(log log n) whereas, it is O(log n) in binary search in the average case scenario.该算法的基本假设之一类似于二进制搜索算法,即必须对元素列表进行排序并使其均匀分布。
它的时间比二进制搜索这里复杂的巨大优势,复杂度为O(log日志N),而,它是O(log n)的在平均的情况下二进制搜索。Input: Array: 10, 20, 35, 45, 55, 68, 88, 91 Element to be searched: 68
Terminologies:
术语:
ub : upper index of the array
ub :数组的上索引
lb : lower index of the array
lb :数组的下标
x : element to be searched
x :要搜索的元素
pos : the position at which the array is split and is calculated using the following formula,
pos :数组拆分的位置,并使用以下公式计算得出:
pos = lb + { [ (ub – lb) / (arr[ub] – arr[lb]) ] * (x – arr[lb]) }
Basic Algorithm:
基本算法:
Find the value of pos using the above formula
使用上面的公式找到pos的值
Compare the pos element with the element to be searched
比较pos元素和要搜索的元素
If the value matches, return the index
如果值匹配,则返回索引
Else if
否则
x is less than the pos element, new sub-array is the elements before the pos element, and if more than the pos value, then the upper half of the array is a new sub-array.
x小于pos元素,新的子数组是pos元素之前的元素,如果大于pos值,则数组的上半部分是新的子数组。
Repeat steps 1-4 till the target is reached or when there are no elements left.
重复步骤1-4,直到达到目标或没有剩余元素为止。
Time Complexity: The time complexities of Interpolation Search Algorithm are,
时间复杂度:插值搜索算法的时间复杂度为
Worst case: O(n)
最坏的情况:O(n)
Average Case: O(log log n)
平均情况:O(log log n)
Best case: O(1), when the element is present at pos itself
最佳情况:O(1),当元素本身位于pos时
Space Complexity: O(1)
空间复杂度:O(1)
C Implementation:
C实现:
#includeint interpol_search(int arr[], int lb, int ub, int x){ while ((arr[lb] != arr[ub]) && (x <= arr[ub]) && (x >= arr[lb])) { int pos = lb + (((ub - lb) / (arr[ub] - arr[lb])) * (x - arr[lb])); if (arr[pos] == x) return pos; if (arr[pos] < x) lb = pos + 1; else ub = pos - 1; } return -1;}int main(){ int arr[] = { 10, 20, 35, 45, 55, 68, 88, 91 }; int n = sizeof(arr) / sizeof(arr[0]); int x = 68; int index = interpol_search(arr, 0, n - 1, x); if (index != -1) printf("Element %d is present at index %d", x, index); else printf("Element %d not found in the list!", x); return 0;}
Output
输出量
Element 68 is present at index 5
翻译自:
线性插值算法实现图像
转载地址:http://isxzd.baihongyu.com/