二进制 & |

Meta Binary Search is a one-sided binary search where we work on the index using bit manipulation. We are to find the target index by using bit manipulation like the below example where the algorithm is explained.

元二进制搜索是一种单面二进制搜索 ,我们在其中使用位操作来处理索引。 我们将通过使用位操作来找到目标索引,如下面的示例(其中解释了该算法)。

Say the input array is [3, 4, 6, 8, 12, 14, 16] and searching key says 14.

假设输入数组为[3、4、6、8、12、14、16] ,搜索键为14

The idea is to figure out the target index. And we will do that using bit manipulation and thus we will use the bit representation of the index. On each iteration, we will set the bits.

想法是找出目标索引。 并且我们将使用位操作来做到这一点,因此我们将使用索引的位表示形式。 在每次迭代中,我们将设置位。

Now the question is, how many bits can be there at maximum in the target index?

现在的问题是, 目标索引中最多可以有多少位?

Taking the worst case, it's the number of bits in the maximum index (array len-1). So for the above example, the maximum number of bits is 3 (max index 6 has 3 bits).

在最坏的情况下,它是最大索引(数组len-1 )中的位数。 因此,对于上面的示例,最大位数为3 (最大索引6为3位)。

Let's describe the bits as x1x2x3

让我们将位描述为x 1 x 2 x 3

Now our work is to assign bits from left (MSB) to the right (LSB), 0, or 1 based on the values they should have. Now how would we assign 0 or 1, we will discuss now.

现在,我们的工作是根据应具有的值从左(MSB)到右(LSB),0或1分配位。 现在我们将如何分配0或1,我们将进行讨论。

First, we will try to put 1 always, if the current index found has value greater searching key then we will put 0, otherwise it's fine to put 1. If we find the current index found has the same value as the key then we are done.

首先,我们将尝试始终放置1,如果找到的当前索引具有更大的搜索键值,则将放置0,否则将放置1。如果找到的当前索引具有与键相同的值,则我们将完成。

So let's start with the example,

让我们从示例开始

The first bit to be set is the 0th bit, MSB
First set it as 1

要设置的一位是第0位MSB
首先将其设置为1

So the current index is 1X2X3 where X2X3 is not either 0, not 1(not set at all). So the minimum current index is right now 100 which is 4(putting 0 in both X2X3 which will lead to minimum index). arr[4] is 12 and it's less than the searching key. Thus it's fine to put 1 at X1

因此,当前索引为1X 2 X 3 ,其中X 2 X 3既不是0,也不是1(根本没有设置)。 因此,最小当前索引现在为100,即4(两个X 2 X 3中都输入0,这将导致最小索引)。 arr [4]为12,小于搜索关键字。 因此,在X1处放1很好

Now it's turn for the second bit.
Let's set it with 1 first

现在轮到第二位了。
首先设置1

So we have 11X3, where X3 is not either 0, not 1(not set at all). So the minimum possible current index is right now 110(6). arr[6] is 16 and it's greater than the searching key. Thus we can't set 1st bit as 1. So we need to set that as 0. X2 =0
Now it's turn for the 2nd bit(LSB).

因此,我们有11X 3 ,其中X 3既不是0,也不是1(根本没有设置)。 因此,最小可能的当前索引现在为110(6)。 arr [6]为16,并且大于搜索关键字。 因此,我们不能将第1位设置为1。因此,我们需要将其设置为0。X 2 = 0
现在轮到第二个位(LSB)了。

Let's set it with 1 first

首先设置1

So we have 101, So the current index is right now 5(101). And, arr[5]=14. We found the searching key and it's at index 5.

所以我们有101,所以当前索引现在是5(101)。 并且,arr [5] = 14。 我们找到了搜索关键字,它位于索引5。

This is how Meta Binary Search works. The time complexity is the number of bits to represent the maximum index which is Log(n).

这就是元二进制搜索的工作方式。 时间复杂度是代表最大索引的位数Log(n)。

During the execution of the algorithm

在执行算法期间

We found that there can be two types of edge cases:

我们发现边缘情况可能有两种:

  1. We may find any target index while executing which is more than the maximum possible index. Say for example the, length of array is 25. Then, the number of bits we need to represent the maximum index possible->5. So while processing we may need to process any index like 11111 which is more than the maximum index possible. Thus we need to keep a check for this kind of scenario.

    我们可能会在执行时发现任何目标索引,该目标索引大于最大可能索引。 假设数组的长度为25。然后,我们需要表示最大可能索引的位数-> 5。 因此,在处理时,我们可能需要处理比11111还要大的任何索引。 因此,我们需要检查这种情况。

  2. Secondly, the searching key may not even exist, in that case, out of the loop, we will return -1 indicating not found.

    其次,搜索键甚至可能不存在,在这种情况下,循环外,我们将返回-1表示未找到。

元二进制搜索算法在C ++中的实现 (Implementation of the meta binary search algorithm in C++)

#include <bits/stdc++.h>
using namespace std;
int logn(int n)
{
int count = 0;
while (n) {
count++;
n >>= 1;
}
return count;
}
int meta_binary_search(vector<int> arr, int key)
{
int n = arr.size();
int no_of_bits = logn(n - 1);
int cur_index = 0;
//iterating log(n) time
for (int shift = no_of_bits - 1; shift >= 0; shift--) {
//set current bit as 1
int new_cur_index = cur_index + 1 << shift;
if (new_cur_index >= n) {
//can't set bit as 1
}
else {
if (arr[new_cur_index] == key)
return new_cur_index;
//we can set bit as 1
else if (arr[new_cur_index] < key) {
cur_index = new_cur_index;
}
}
}
//not found
return -1;
}
int main()
{
vector<int> arr{ 3, 4, 6, 8, 12, 14, 16 };
int key = 12;
//key found case
int index = meta_binary_search(arr, key);
if (index != -1)
cout << "key " << key << " found at: " << index << endl;
else
cout << "key " << key << " not found\n";
//key not found case
key = 9;
index = meta_binary_search(arr, key);
if (index != -1)
cout << "key " << key << " found at: " << index << endl;
else
cout << "key " << key << " not found\n";
return 0;
}

Output:

输出:

key 12 found at: 4
key 9 not found

翻译自: https://www.includehelp.com/algorithms/meta-binary-search-one-sided-binary-search.aspx

二进制 & |

二进制 |_元二进制搜索| 单边二元搜索相关推荐

  1. 二元矩阵峰值搜索_好斗的牛(二元搜索)

    二元矩阵峰值搜索 A farmer has built a long barn with N stalls. The stalls are placed in a straight manner at ...

  2. 实现搜索框记录搜索历史_三个案例告诉你:“搜索框”该如何设计?

    不管是在2C还是2B产品,"搜索"是产品中最常见且必不可少的功能模块之一.对于用户来说,在众多功能模块和信息层级中如何快速定位到目标,通过输入已知的关键词进行搜索是最便捷的方式,没 ...

  3. 为什么地磅的读数有进制么_谈谈二进制(三)——位运算及其应用

    0. 概要 前两篇文章我们了解了二进制的基本原理(谈谈二进制(一))以及二进制的四则运算(谈谈二进制(二)),本篇我们一起来看看二进制的位运算.先来看一下有哪些位运算: 上表中列出了我们编程语言中的所 ...

  4. matlab超出矩阵索引维度_搜你想看“头条搜索”网页版上线 搜索引擎迎来新玩家...

    作者:七声 审校:一条辉 来源:GPLP犀牛财经(ID:gplpcn) 千呼万唤始出来,8月10日,今日头条旗下"头条搜索"网页版正式上线,slogan为"搜你想看&qu ...

  5. 正在搜索需要的文件一直在搜索_正在被蚕食的百度搜索

    28.03.2019 本文字数:3118,阅读时长大约6分钟 导读:百度搜索在国内一家独大的格局虽暂时无人撼动,但愈发饱和的流量市场背后,搜索业务正呈现出多元化与差异化发展的趋势. 作者 | 第一财经 ...

  6. -32767转化为二进制_谁说前端不需要懂二进制

    (给前端大全加星标,提升前端技能) 作者:全栈成长之路 公号 / 山月行 作为一名前端,在工作中也会遇到很多有关二进制处理的需求,如 EXCEL 表格的导出,PDF 的生成,多个文件的打包,音频的处理 ...

  7. 有趣的二进制_软件安全与逆向分析

    有趣的二进制_软件安全与逆向分析 [日] 爱甲健二/著 周自恒/译 链接: https://pan.baidu.com/s/1mJKG01xq7gVYcTQmZL3Z_A 提取码: gmp2 都是买的 ...

  8. 学习笔记_使用二进制的方式修改图片

    学习笔记_使用二进制的方式修改图片 1.学习目标 2.学习工具 3.开始操作 3.1 了解图片是怎样用二进制表示的 3.2 开始操作 4.参考文档 1.学习目标 掌握使用二进制的方式修改图片 了解BM ...

  9. seo网站关键词优化-搜索词和搜索结果观察_百度搜索

    本篇通过2个搜索内容来介绍一下百度搜索内容和搜索结果之间的关系观察. 话不多说直捣黄龙. 例1:在百度首页搜索框中输入"200x100热镀锌槽式直通价格"进行搜索 1.搜索内容分词 ...

最新文章

  1. 扛住100亿次请求?我们来试一试!
  2. 我所理解的原型原型链 1
  3. windows下磁盘IO性能数据评测
  4. Cpp / #error、static_assert、assert 区别
  5. C++ 暴力搜索String pattern search字符串模式的实现算法(附完整源码)
  6. word、excel、ppt 办公文件 在线预览
  7. SpringBoot中 pagehelper插件使用
  8. Radware;医疗行业数字转型5大关键注意事项
  9. [原译]Lambda高手之路第二部分
  10. python怎么理解函数的参数_Python中函数参数理解
  11. Paip.最佳实践-- Buildin variale 内建变量 ,魔术变量,预定义变量,系统常量,系统变量 1
  12. 中英文国际机场三字代码
  13. 知名大企业的愿景和使命
  14. mov转换成mp4,详细步骤
  15. 3097: Hash Killer I
  16. 【CodeForces 697C】Lorenzo Von Matterhorn(LCA)
  17. 关于5G数据中心:数据中心在5G无线网络系统中的作用
  18. Unresolveable build extension: Plugin...or one of its dependencies could not be resolved: Failed to
  19. vue打开新的标签页
  20. tensorflow的数据类型

热门文章

  1. 江西财经大学计算机排名2019,2019年全国商科院校评价报告出炉 江西财经大学排名第七...
  2. sql中in与php数组,格式化SQL“IN”子句的PHP数组
  3. android wifi设备连接通信,通过wifi与设备进行通信(Android)
  4. http 错误 404.0 - not found_电脑Regsvr32 用法和错误消息的说明
  5. 在导入NVIDIA的apex库时报错 ImportError cannot import name ‘UnencryptedCookieSessionFactoryConfig‘ from
  6. php无限评论回复_php实现无限级评论功能_后端开发
  7. python是强定义语言吗_python是强类型语言吗
  8. css span 右端对齐_CSS标准文档流
  9. 四因素三水平正交表_做论文要用正交表?我打包送给你
  10. redis核心技术与实战(四)高可用高扩展篇