博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线性插值算法实现图像_C程序实现插值搜索算法
阅读量:2531 次
发布时间:2019-05-11

本文共 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 )

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:

基本算法:

  1. Find the value of pos using the above formula

    使用上面的公式找到pos的值

  2. Compare the pos element with the element to be searched

    比较pos元素和要搜索的元素

  3. 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值,则数组的上半部分是新的子数组。

  4. 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实现:

#include 
int 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/

你可能感兴趣的文章
linux 系统下 tar 的压缩与解压缩命令
查看>>
阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
UVa 1658,Admiral (拆点+限制最小费用流)
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
音视频处理
查看>>
Careercup - Facebook面试题 - 5890898499993600
查看>>
浮点数在内存中的存储方式
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>
Jenkins安装配置
查看>>
个人工作总结05(第二阶段)
查看>>
Java clone() 浅拷贝 深拷贝
查看>>
深入理解Java虚拟机&运行时数据区
查看>>
STUCTS LABLE ‘S BENEFIT
查看>>
Vue 计算属性
查看>>
第十八次ScrumMeeting博客
查看>>