线性插值插值_揭秘插值搜索
線性插值插值
搜索算法指南 (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.
在本文之前,我已經撰寫了有關Binary Search的文章 。 如果您沒有看過,請檢查一下。 在本文中,我們將討論插值搜索,它是對已排序數組中的值進行均勻分布時二進制搜索的一種改進。
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.
您可能還記得二進制搜索已經以O(log n)時間復雜度運行。 還能有其他方法表現更好嗎? 嗯...部分是。 我們將在短期內深入探討。
什么是插值搜索? (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.
插值搜索是另一種有效的搜索技術。 在數組元素均勻分布的情況下,此方法可以勝過二進制搜索。 插值搜索估計鍵值在數組中的位置。 這可以通過考慮數組中最小和最大的元素以及數組的長度來實現。
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.
基于以下想法:如果鍵值較大(更靠近最大元素),則其位置可能位于數組的末尾。 具有較小值的鍵也是如此。
它是如何工作的? (How Does it Work?)
There are the 6 main steps in doing Interpolation Search, which are:
進行插值搜索有6個主要步驟,它們是:
插值搜索示例 (Interpolation Search Example)
As usual, the pseudo-code will be provided first before we move to the implementation in different programming languages.
與往常一樣,在我們使用不同的編程語言來實現之前,將首先提供偽代碼。
Interpolation Search Pseudo-Code
插值搜索偽代碼
Now, we will move on to the real code for 3 different languages, Python, C, and JavaScript.
現在,我們將繼續使用3種不同語言(Python,C和JavaScript)的真實代碼。
Interpolation Search Code in Python
Python中的插值搜索代碼
Interpolation Search Code in C
C語言中的插補搜索代碼
Interpolation Search Code in JavaScript
JavaScript中的插值搜索代碼
插值搜索的時間復雜度 (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.
在較小的數組中,插值搜索比二進制搜索慢。 其背后的原因是插值搜索需要更多的計算。 但是,較大的數組和均勻分布的數組是插值搜索的強項。 與二值搜索相比,插值搜索時間復雜度的增長率較小。
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).
當中間(我們的近似值)是所需的關鍵點時,就會發生插值搜索的最佳情況。 最好的情況是時間復雜度為O(1)。 在最壞的情況下,我們將需要遍歷數組中的所有元素,從而導致O(n)時間復雜度。 好消息是對于一般情況,時間復雜度小至O(log log n)。
結論 (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:
總結一下,學習如何應用插值搜索不僅可以豐富您的算法知識,還可以作為一名程序員。 毫無疑問,它將在您的編程工具包中占有一席之地。 但是,應始終記住:
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.
成為專家程序員沒有簡單的方法,唯一可行的途徑是持續不斷地逐一學習關鍵概念。 因此,如果我能理解這些概念,那么您也可以。
“The secret to getting ahead is getting started.”
“取得成功的秘訣就是開始。”
— Mark Twain
—馬克·吐溫
Let’s go to the top together!
讓我們一起去頂吧!
Regards,
問候,
Radian Krisno
拉迪安·克里斯諾(Radian Krisno)
翻譯自: https://towardsdatascience.com/demystifying-interpolation-search-45dca5c24115
線性插值插值
總結
以上是生活随笔為你收集整理的线性插值插值_揭秘插值搜索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到汽车在水里开过预示什么
- 下一篇: 如果您不将Docker用于数据科学项目,