当前位置:网站首页>Which is more efficient, interpolation search method or binary search method?
Which is more efficient, interpolation search method or binary search method?
2022-07-20 09:09:00 【JYCJ_】
What is? Interpolation search ?
Before introducing interpolation search , Let me first introduce the binary search method , Then combine the comparison between the two , You will understand .
At ordinary times , We often use binary search , To quickly find whether the specified element is in a collection ( Premise is : The set has been ordered )
Two points search
First declare the following variables :
Left position :left = 0, Right position : right = len(list) - 1
Find the target element : target
Sets are arranged in ascending order ;
Find the specific process :
1. Calculate the middle position : mid = (left + right)/ 2
2. By comparison target And list [mid] Size to constantly update left and right
- target == list [mid], The goal is target find
- target > list [mid], The goal is target stay mid Position right , to update : left = mid + 1
- target < list [ mid ], The goal is target stay mid Position left , to update : right = mid - 1
3. repeat 1 step , Until the cycle conditions are not met : left <= right
What is Interpolation search
Interpolation search
The difference between it and binary search is : mid How to calculate the position
that Interpolation search How to calculate the position of further comparison ?
The specific process is :
// a The calculation of can be understood as : take left And right Spacing between , Divide equally according to the difference between the rightmost and leftmost elements
a = int(float64(right - left) / float64( list [ right ] - list [ left ]))
// b The calculation of can be understood as : target The actual difference from the leftmost element
b = target - list [ left ]
// Final position That is to say : The leftmost position add Actual offset interval ( a * b)
position = left + a * b
The result here is position Equivalent to In binary search mid,
This is a variable speed method of finding position , Efficiency is higher than binary search .
Hope you found something .
See you next time
边栏推荐
- 108 Polkadot substrate: private network
- 10I Polkadot substrate: connect local parallel chains
- Homebrew的使用
- 分布式事务框架 Seata 与 Hmily 横向比较
- 列表(list)、元组(tuple)、字典(dict)、集合(set)的使用
- js Qrcode.js实现文字内容通过二维码展示
- 10q Polkadot substrate: use map to store values
- CPU架构兼容
- Without programming, generate crud based on zero code of Oracle database, add, delete, modify and check restful API interface
- The problem of data set CSV coding format in machine learning
猜你喜欢
OpenCV:07图像轮廓
MPPT电源控制器设计
ShardingJDBC
Array 常用方法原理模拟,以及常用函数高阶
margin 合并问题总结
What if I forget my MySQL password?
Without programming, generate crud based on zero code of Oracle database, add, delete, modify and check restful API interface
Talking about wechat payment risk control
使用Cocos Creator制作试玩广告(PlayableAd)
分布式事务框架 Seata 与 Hmily 横向比较
随机推荐
JS中 绑定事件总结 以及注意事项
机器学习中数据集csv编码格式问题
富文本设置图片大小和字体大小
[]int64 -> []interface{}
Development trend and advantages of third-party payment
Seata XA 模式示例分析
求 top k有哪些方法
108 Polkadot substrate: private network
MYSQL如何正确使用limit offset
Excel 错误含义
百度飞桨七天训练营结营总结
项目中遇到的一些问题或异常以及处理方法
Static library A documents and Framework file
10A Polkadot substrate: node index
企业微信自建应用
JVM online problem tracking
模拟图像加密、解密
YOLOv3详解
crudapi增删改查接口零代码产品成功案例之商会联盟卡项目
EXCEL中VLOOKUP的使用