線性插值插值_揭秘插值搜索

線性插值插值

搜索算法指南 (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個主要步驟,它們是:

  1. Estimates the middle position for the array and compares this element to the desired key.

    估計數組的中間位置,并將此元素與所需鍵進行比較。
  2. If the key matches the middle element, return the middle location.

    如果鍵與中間元素匹配,則返回中間位置。
  3. If the key is smaller than the middle element, then the key can only lie in the left subarray. Focus on the left subarray

    如果鍵小于中間元素,則鍵只能位于左子數組中。 專注于左側子數組
  4. Else, we move our focus only to the right subarray.

    否則,我們僅將焦點移到正確的子數組上。
  5. Repeat steps 1, 2, 3, and 4 until the desired key is found or every subarray has been checked.

    重復步驟1、2、3和4,直到找到所需的鍵或已檢查每個子數組。
  6. 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:

總結一下,學習如何應用插值搜索不僅可以豐富您的算法知識,還可以作為一名程序員。 毫無疑問,它將在您的編程工具包中占有一席之地。 但是,應始終記住:

  1. Similar to Binary Search, Interpolation Search only works in sorted arrays.

    與二進制搜索類似,插值搜索僅適用于排序數組。
  2. 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,一經查實,立即刪除!

相關文章

其他命令

keys *這個可以全部的值del name 這個可以刪除某個127.0.0.1:6379> del s_set(integer) 1127.0.0.1:6379> keys z*(匹配)1) "z_set2"2) "z_set"127.0.0.1:6379> exists sex(integer) 0 127.0.0.1:6379> get a"3232…

建按月日自增分區表

一、建按月自增分區表: 1.1建表SQL> create table month_interval_partition_table (id number,time_col date) partition by range(time_col)2 interval (numtoyminterval(1,month))3 (4 partition p_month_1 values less than (to_date(2014-01-01,yyyy-mm…

#1123-JSP隱含對象

JSP 隱含對象 JSP隱含對象是JSP容器為每個頁面提供的Java對象,開發者可以直接使用它們而不用顯式聲明。JSP隱含對象也被稱為預定義變量。 JSP所支持的九大隱含對象: 對象,描述 request,HttpServletRequest類的實例 response&#…

按照時間,每天分區;按照數字,200000一個分區

按照時間,每天分區 create table test_p(id number,createtime date) partition by range(createtime) interval(numtodsinterval(1,day)) store in (users) ( partition test_p_p1 values less than(to_date(20140110,yyyymmdd)) ); create index index_test_p_id …

如果您不將Docker用于數據科學項目,那么您將生活在1985年

重點 (Top highlight)One of the hardest problems that new programmers face is understanding the concept of an ‘environment’. An environment is what you could say, the system that you code within. In principal it sounds easy, but later on in your career yo…

jmeter對oracle壓力測試

下載Oracle的jdbc數據庫驅動包,注意Oracle數據庫的版本,這里使用的是:Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production; 一般數據庫的驅動包文件在安裝路徑下:D:\oracle\product\10.2.…

集合里面的 E是泛型 暫且認為是object

集合里面的 E是泛型 暫且認為是object轉載于:https://www.cnblogs.com/classmethond/p/10011374.html

docker部署flask_使用Docker,GCP Cloud Run和Flask部署Scikit-Learn NLP模型

docker部署flaskA brief guide to building an app to serve a natural language processing model, containerizing it and deploying it.構建用于服務自然語言處理模型,將其容器化和部署的應用程序的簡要指南。 By: Edward Krueger and Douglas Franklin.作者&am…

異常處理的原則

1:函數內部如果拋出需要檢測的異常,那么函數上必須要聲明,否則必須在函數內用try catch捕捉,否則編譯失敗。2:如果調用到了聲明異常的函數,要么try catch 要么throws,否則編譯失敗。3&#xff…

模塊化整理

#region常量#endregion#region 事件#endregion#region 字段#endregion#region 屬性#endregion#region 方法#endregion#region Unity回調#endregion#region 事件回調#endregion#region 幫助方法#endregion來自為知筆記(Wiz)轉載于:https://www.cnblogs.com/soviby/p/10013294.ht…

在oracle中處理日期大全

在oracle中處理日期大全 TO_DATE格式 Day: dd number 12 dy abbreviated fri day spelled out friday ddspth spelled out, ordinal twelfth Month: mm number 03 mon abbreviated mar month spelled out march Year: yy two digits 98 yyyy four …

BZOJ4868 Shoi2017期末考試(三分+貪心)

容易想到枚舉最晚發布成績的課哪天發布,這樣與ti和C有關的貢獻固定。每門課要么貢獻一些調節次數,要么需要一些調節次數,剩下的算貢獻也非常顯然。這樣就能做到平方級別了。 然后大膽猜想這是一個凸函數三分就能A掉了。具體的,延遲…

SQL的執行計劃

SQL的執行計劃實際代表了目標SQL在Oracle數據庫內部的具體執行步驟,作為調優,只有知道了優化器選擇的執行計劃是否為當前情形下最優的執行計劃,才能夠知道下一步往什么方向。 執行計劃的定義:執行目標SQL的所有步驟的組合。 我們首…

問卷 假設檢驗 t檢驗_真實問題的假設檢驗

問卷 假設檢驗 t檢驗A statistical Hypothesis is a belief made about a population parameter. This belief may or might not be right. In other words, hypothesis testing is a proper technique utilized by scientist to support or reject statistical hypotheses. Th…

webpack打包ES6降級ES5

Babel是一個廣泛使用的轉碼器,babel可以將ES6代碼完美地轉換為ES5代碼,所以我們不用等到瀏覽器的支持就可以在項目中使用ES6的特性。 安裝babel實現ES6到ES5 npm install -D babel-core babel-preset-es2015 復制代碼安裝babel-loader npm install -D ba…

[轉帖]USB-C和Thunderbolt 3連接線你搞懂了嗎?---沒搞明白.

USB-C和Thunderbolt 3連接線你搞懂了嗎? 2018年11月25日 07:30 6318 次閱讀 稿源:威鋒網 3 條評論按照計算行業的風潮,USB Type-C 將會是下一代主流的接口。不過,在過去兩年時間里,關于 USB-C、Thunderbolt 3、USB 3.1…

sqldeveloper的查看執行計劃快捷鍵F10

簡介:本文全面詳細介紹oracle執行計劃的相關的概念,訪問數據的存取方法,表之間的連接等內容。并有總結和概述,便于理解與記憶!目錄---一.相關的概念Rowid的概念Recursive Sql概念Predicate(謂詞)DRiving Table(驅動表)…

大數據技術 學習之旅_為什么聚焦是您數據科學之旅的關鍵

大數據技術 學習之旅David Robinson, a data scientist, has said the following quotes:數據科學家David Robinson曾說過以下話: “When you’ve written the same code 3 times, write a function.”“當您編寫了3次相同的代碼時,請編寫一個函數。” …

SQL 語句

去重字段里的值 SELECT DISTINCT cat_id,goods_sn,repay FROM ecs_goods where cat_id ! 20014 刪除除去 去重字段 DELETE FROM ecs_goods where goods_id NOT IN ( select bid from (select min(goods_id) as bid from ecs_goods group by cat_id,goods_sn,repay) as b );轉…

無監督學習 k-means_無監督學習-第4部分

無監督學習 k-means有關深層學習的FAU講義 (FAU LECTURE NOTES ON DEEP LEARNING) These are the lecture notes for FAU’s YouTube Lecture “Deep Learning”. This is a full transcript of the lecture video & matching slides. We hope, you enjoy this as much as …