線性插值插值
搜索算法指南 (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個主要步驟,它們是:
- Estimates the middle position for the array and compares this element to the desired key. 估計數組的中間位置,并將此元素與所需鍵進行比較。
- If the key matches the middle element, return the middle location. 如果鍵與中間元素匹配,則返回中間位置。
- If the key is smaller than the middle element, then the key can only lie in the left subarray. Focus on the left subarray 如果鍵小于中間元素,則鍵只能位于左子數組中。 專注于左側子數組
- Else, we move our focus only to the right subarray. 否則,我們僅將焦點移到正確的子數組上。
- Repeat steps 1, 2, 3, and 4 until the desired key is found or every subarray has been checked. 重復步驟1、2、3和4,直到找到所需的鍵或已檢查每個子數組。
- If there is no match in the array, return -1 如果數組中沒有匹配項,則返回-1
插值搜索示例 (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:
總結一下,學習如何應用插值搜索不僅可以豐富您的算法知識,還可以作為一名程序員。 毫無疑問,它將在您的編程工具包中占有一席之地。 但是,應始終記住:
- Similar to Binary Search, Interpolation Search only works in sorted arrays. 與二進制搜索類似,插值搜索僅適用于排序數組。
- You need to have direct access to the elements of the array. 您需要直接訪問數組的元素。
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
線性插值插值
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。 如若轉載,請注明出處:http://www.pswp.cn/news/387915.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/387915.shtml 英文地址,請注明出處:http://en.pswp.cn/news/387915.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!