二進制 |_元二進制搜索| 單邊二元搜索

二進制 & |

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.pswp.cn/news/541786.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541786.shtml
英文地址,請注明出處:http://en.pswp.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…