线性插值算法实现图像_C程序实现插值搜索算法
線性插值算法實現(xiàn)圖像
Problem:
問題:
We are given an array arr[] with n elements and an element x to be searched amongst the elements of the array.
給定一個數(shù)組arr [],其中包含n個元素和一個要在該數(shù)組的元素中搜索的元素x 。
Solution:
解:
Here, we will be performing Interpolation Search to find the element in the array.
在這里,我們將執(zhí)行插值搜索以找到數(shù)組中的元素。
插值搜索 (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.
插值搜索是比線性和二進(jìn)制搜索更好的搜索算法選擇。 我們可以對非常小的數(shù)據(jù)集執(zhí)行線性搜索,對大型數(shù)據(jù)集執(zhí)行二進(jìn)制搜索,但是如果給定的數(shù)據(jù)集包含數(shù)百萬個數(shù)據(jù)行和列,該怎么辦。 在這里,插值搜索可以解決。
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.
該算法的基本假設(shè)之一類似于二進(jìn)制搜索算法,即必須對元素列表進(jìn)行排序并使其均勻分布。
它的時間比二進(jìn)制搜索這里復(fù)雜的巨大優(yōu)勢,復(fù)雜度為O(log日志N),而,它是O(log n)的在平均的情況下二進(jìn)制搜索。
Terminologies:
術(shù)語:
ub : upper index of the array
ub :數(shù)組的上索引
lb : lower index of the array
lb :數(shù)組的下標(biāo)
x : element to be searched
x :要搜索的元素
pos : the position at which the array is split and is calculated using the following formula,
pos :數(shù)組拆分的位置,并使用以下公式計算得出:
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元素,新的子數(shù)組是pos元素之前的元素,如果大于pos值,則數(shù)組的上半部分是新的子數(shù)組。
Repeat steps 1-4 till the target is reached or when there are no elements left.
重復(fù)步驟1-4,直到達(dá)到目標(biāo)或沒有剩余元素為止。
Time Complexity: The time complexities of Interpolation Search Algorithm are,
時間復(fù)雜度:插值搜索算法的時間復(fù)雜度為
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),當(dāng)元素本身位于pos時
Space Complexity: O(1)
空間復(fù)雜度:O(1)
C Implementation:
C實現(xiàn):
#include <stdio.h>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;elseub = 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);elseprintf("Element %d not found in the list!", x);return 0; }Output
輸出量
Element 68 is present at index 5翻譯自: https://www.includehelp.com/c-programs/implement-interpolation-search-algorithm.aspx
線性插值算法實現(xiàn)圖像
總結(jié)
以上是生活随笔為你收集整理的线性插值算法实现图像_C程序实现插值搜索算法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 合肥治疗精子活率低最好的医院推荐
- 下一篇: c ++明明的随机数_从列表C ++程序