二进制 |_元二进制搜索| 单边二元搜索

二进制 & |

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

二进制 & |

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541786.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

codeMirror配置

介绍 CodeMirror是一款在线的支持语法高亮的代码编辑器。官网&#xff1a;http://codemirror.net/ 下载后&#xff0c;解压开到的文件夹中&#xff0c;lib下是放的是核心库和核心css&#xff0c;模式下放的是各种支持语言的语法定义&#xff0c;主题目录下是支持的主题样式。一…

应梦框架9.0框架_.Net框架能力问题和解答

应梦框架9.0框架This section contains Aptitude Questions and Answers on .Net Framework. 本节包含有关.Net Framework的能力问题和解答。 1) There are the following options are given below, what are parts of the .NET Framework? FCL (Framework Class Library)Web…

php中文网视频放不了,【杂谈】看php中文网视频课程的正确姿势!

看在线课程如何集中精力学习&#xff1f;ki4网为你分享看ki4网视频课程的正确姿势&#xff01;不谈理论给些实用建议&#xff0c;可以根据你的情况多尝试&#xff0c;看看哪条对你有用&#xff01;1、选一门自己有兴趣而且教师讲得好的课程。(点击学习&#xff1a;ki4网视频教程…

算法笔记_065:分治法求逆序对(Java)

目录 1 问题描述 2 解决方案 2.1 蛮力法 2.2 分治法&#xff08;归并排序&#xff09; 1 问题描述 给定一个随机数数组&#xff0c;求取这个数组中的逆序对总个数。要求时间效率尽可能高。 那么&#xff0c;何为逆序对&#xff1f; 引用自百度百科&#xff1a; 设 A 为一个有 n…

c#copyto_String.CopyTo()方法以及C#中的示例

c#copytoC&#xff03;String.CopyTo()方法 (C# String.CopyTo() Method) String.CopyTo() method is used to copy a specified number of characters from given indexes of the string to the specified position in a character array. String.CopyTo()方法用于将指定数量的…

怎么查看我的php版本,怎样查看php版本

怎样查看php版本方法一&#xff1a;命令行查询如果已经配置好环境变量&#xff0c;直接在命令行中输入php -v&#xff0c;将会显示php的版本信息。如果没有配置环境变量&#xff0c;直接在命令行中进入到php的安装目录后&#xff0c;再输入命令php -v&#xff0c;如图所示是我在…

c ++ 继承_C ++继承| 查找输出程序| 套装1

c 继承Program 1: 程序1&#xff1a; #include <iostream>#include <string.h>using namespace std;class Person {char name[15];int age;public:void SetPerson(int age, char* name){this->age age;strcpy(this->name, name);}};class Student : publi…

xor在PHP是什么意思,?=‘在PHP中是什么意思?

万千封印因为它不会增加任何价值echo&#xff0c;我认为您希望了解PHP中的确切含义&#xff1a;Array([0] > Array([0] > 368 // T_OPEN_TAG_WITH_ECHO[1] > [2] > 1)[1] > Array([0] > 309 // T_VARIABLE[1] > $a [2] > 1)[2] > ; // U…

php curl keepalive,HTTPKeepAlive,开启还是关闭

所谓「HTTP Keep-Alive」&#xff0c;在维基百科里称为「HTTP Persistent Connection」&#xff0c;说白了就是复用HTTP连接&#xff0c;如此一来理论上客户端的用户体验会更流畅&#xff0c;但是与之相对服务端不得不维持大量的连接。开启还是关闭&#xff0c;这是个问题。一个…

如何使用ES6中的参数

ECMAScript 6&#xff08;或者叫 ECMAScript 2015&#xff09;是 ECMAScript 的最新标准&#xff0c;极大的提高了 JavaScript 中处理参数的能力。现在我们可以使用 rest 参数&#xff08;rest parameters&#xff09;、默认值&#xff08;default values&#xff09;和解构&am…

c++中tle是什么意思_如何在竞争性编程中克服TLE?

c中tle是什么意思什么是TLE&#xff1f; (What is TLE?) TLE means "Time Limit Exceed". So, in competitive programming, there are some constraints with a specific time limit (normally for each input 1 sec) and your task is to write your code in such…

美颜相机window 开源_X-Window系统| 免费和开源软件

美颜相机window 开源X窗口系统 (The X-Window System) The X-Window System is a GUI that sits over Linux. Not at all like Microsoft Windows, the X Window System can glance and work in an enormously wide range of ways. It can work smoothly or lag, look excellen…

php 代码 自动检查工具下载,PHP_CodeSniffer安装和使用教程(自动代码检查规范工具)...

在我们开发中都会讲究代码规范&#xff0c;若是个人开发者&#xff0c;代码规范与否&#xff0c;只要自己看得懂便可以了&#xff0c;但是在团队协作中&#xff0c;代码规定尤为重要&#xff0c;下面&#xff0c;我们介绍一款PHP_CodeSniffer&#xff0c;自动检查代码规范的工具…

国际象棋之跳马程序

问题描述: 假设国际象棋棋盘有5*5共25个格子。设计一个程序,使棋子从初始位置&#xff08;棋盘格编号为1的位置)开始跳马,能够把棋盘的格子全部走一遍,每个格子只允许走一次。要求: 1) 输出一个解(用二维数组来记录马跳的过程,即[步号,棋盘格编号],左上角为第一步起点)&#xf…

kafka安装使用

版本&#xff1a;kafka_2.11-0.10.1.0 (之前安装2.10-0.10.0.0&#xff0c;一直出问题) 安装Springboot结合Kafka的使用安装 下载并解压代码 wget http://mirrors.cnnic.cn/apache/kafka/0.10.0.0/kafka_2.10-0.10.0.0.tgz #http://kafka.apache.org/downloadstar -zxvf kafka…

php获取上传文件路径 fakepath,JavaScript_js获取上传文件的绝对路径实现方法,在html中input type=file - phpStudy...

js获取上传文件的绝对路径实现方法在html中function upload() {var filename document.getElementById("importFile").value;// 这时的filename不是 importFile 框中的值alert(filename);}如上面的代码&#xff0c;用文件上传对话框选择文件后&#xff0c;如果选择&…

在Bootstrap中使用类的按钮类型

Bootstrap has 7 different types of buttons with contextual classes from which we can create buttons easily by using these classes (.btn-default, .btn-success, .btn-danger, .btn-primary, .btn-info, .btn-warning, .btn-link). Bootstrap具有上下文类型的 7种不同…

php json encode中文乱码,php json_encode中文乱码如何解决

php encode中文乱码的解决办法&#xff1a;首先打开相应的PHP文件&#xff1b;然后使用正则语句“preg_replace("#\\\u([0-9a-f]{4})#ie","iconv(UCS-2BE, UTF-8...)”将编码替换成中文即可。本文列举3个方法&#xff0c;实现json_encode()后的string显示中文问…

乡村图景(转载)

转自: http://cul.qq.com/a/20160205/046437.htm 我丈夫家在湖北孝感孝昌县的一个村子。2005年第一次过年回到他家&#xff0c;印象最深的就是嫂子。我暗自问当时的男友&#xff0c;“哥哥尽管算不上特别帅气&#xff0c;但为何找了这么难看的嫂子&#xff1f;”后来才发现&…

stl向量最大值_C ++ STL中向量的最小和最大元素

stl向量最大值Given a vector and we have to find the smallest (minimum) and largest (maximum) elements. 给定一个向量&#xff0c;我们必须找到最小(最小)和最大(最大)元素。 查找向量的最小和最大元素 (Finding vectors minimum & maximum elements) To find minim…