线性插值插值_揭秘插值搜索
線性插值插值
搜索算法指南 (Searching Algorithm Guide)
Prior to this article, I have written about Binary Search. Check it out if you haven’t seen it. In this article, we will be discussing Interpolation Search, which is an improvement of Binary Search when the values in the sorted array are uniformly distributed.
在本文之前,我已經(jīng)撰寫了有關(guān)Binary Search的文章 。 如果您沒有看過(guò),請(qǐng)檢查一下。 在本文中,我們將討論插值搜索,它是對(duì)已排序數(shù)組中的值進(jìn)行均勻分布時(shí)二進(jìn)制搜索的一種改進(jìn)。
You might recall that Binary Search is already running in O(log n) time complexity. Can there be any other methods that perform even better? Ummm… partly yes. We will dive into it shortly.
您可能還記得二進(jìn)制搜索已經(jīng)以O(shè)(log n)時(shí)間復(fù)雜度運(yùn)行。 還能有其他方法表現(xiàn)更好嗎? 嗯...部分是。 我們將在短期內(nèi)深入探討。
什么是插值搜索? (What is Interpolation Search?)
Interpolation Search is another efficient searching technique. This method can outrun binary search in cases where the elements of the array are uniformly distributed. Interpolation Search estimates the position of the key value in the array. This can be achieved by taking into account the smallest and the largest element in the array and the length of the array.
插值搜索是另一種有效的搜索技術(shù)。 在數(shù)組元素均勻分布的情況下,此方法可以勝過(guò)二進(jìn)制搜索。 插值搜索估計(jì)鍵值在數(shù)組中的位置。 這可以通過(guò)考慮數(shù)組中最小和最大的元素以及數(shù)組的長(zhǎng)度來(lái)實(shí)現(xiàn)。
It is based on the thinking that if the key value is larger (closer to the largest element), it’s position is likely located to the end of the array. The same thing goes for a key with a smaller value.
基于以下想法:如果鍵值較大(更靠近最大元素),則其位置可能位于數(shù)組的末尾。 具有較小值的鍵也是如此。
它是如何工作的? (How Does it Work?)
There are the 6 main steps in doing Interpolation Search, which are:
進(jìn)行插值搜索有6個(gè)主要步驟,它們是:
插值搜索示例 (Interpolation Search Example)
As usual, the pseudo-code will be provided first before we move to the implementation in different programming languages.
與往常一樣,在我們使用不同的編程語(yǔ)言來(lái)實(shí)現(xiàn)之前,將首先提供偽代碼。
Interpolation Search Pseudo-Code
插值搜索偽代碼
Now, we will move on to the real code for 3 different languages, Python, C, and JavaScript.
現(xiàn)在,我們將繼續(xù)使用3種不同語(yǔ)言(Python,C和JavaScript)的真實(shí)代碼。
Interpolation Search Code in Python
Python中的插值搜索代碼
Interpolation Search Code in C
C語(yǔ)言中的插補(bǔ)搜索代碼
Interpolation Search Code in JavaScript
JavaScript中的插值搜索代碼
插值搜索的時(shí)間復(fù)雜度 (Time Complexity of Interpolation Search)
In smaller arrays, Interpolation Search is slower than Binary Search. The reason behind this is Interpolation Search requires more computations. However, larger arrays and the ones that are uniformly distributed are Interpolation Search’s forte. The growth rate of Interpolation Search time complexity is smaller compared to Binary Search.
在較小的數(shù)組中,插值搜索比二進(jìn)制搜索慢。 其背后的原因是插值搜索需要更多的計(jì)算。 但是,較大的數(shù)組和均勻分布的數(shù)組是插值搜索的強(qiáng)項(xiàng)。 與二值搜索相比,插值搜索時(shí)間復(fù)雜度的增長(zhǎng)率較小。
The best case for Interpolation Search happens when the middle (our approximation) is the desired key. This makes the best case time complexity is O(1). In the worst-case scenario, we will need to traverse all of the elements in the array, resulting in the O(n) time complexity. The good news is for the average case, the time complexity is as small as O(log log n).
當(dāng)中間(我們的近似值)是所需的關(guān)鍵點(diǎn)時(shí),就會(huì)發(fā)生插值搜索的最佳情況。 最好的情況是時(shí)間復(fù)雜度為O(1)。 在最壞的情況下,我們將需要遍歷數(shù)組中的所有元素,從而導(dǎo)致O(n)時(shí)間復(fù)雜度。 好消息是對(duì)于一般情況,時(shí)間復(fù)雜度小至O(log log n)。
結(jié)論 (Conclusion)
Summing up, learning how to apply Interpolation Search will enrich your knowledge not only about algorithms but also as a programmer. Undoubtedly, it will have a place in your programming toolkit. However, it should always be remembered:
總結(jié)一下,學(xué)習(xí)如何應(yīng)用插值搜索不僅可以豐富您的算法知識(shí),還可以作為一名程序員。 毫無(wú)疑問(wèn),它將在您的編程工具包中占有一席之地。 但是,應(yīng)始終記住:
There is no easy way to become an expert programmer, the only available path is by persistently learning about the key concepts one by one. So, if I can understand these concepts, so can you.
成為專家程序員沒有簡(jiǎn)單的方法,唯一可行的途徑是持續(xù)不斷地逐一學(xué)習(xí)關(guān)鍵概念。 因此,如果我能理解這些概念,那么您也可以。
“The secret to getting ahead is getting started.”
“取得成功的秘訣就是開始。”
— Mark Twain
—馬克·吐溫
Let’s go to the top together!
讓我們一起去頂吧!
Regards,
問(wèn)候,
Radian Krisno
拉迪安·克里斯諾(Radian Krisno)
翻譯自: https://towardsdatascience.com/demystifying-interpolation-search-45dca5c24115
線性插值插值
總結(jié)
以上是生活随笔為你收集整理的线性插值插值_揭秘插值搜索的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到汽车在水里开过预示什么
- 下一篇: 做梦梦到自己辞职了是什么意思