查找算法之元二分搜索|单边二分搜索
创始人
2025-05-30 04:46:17

元二分搜索(也被称为单边二分搜索)是一种改进的二分搜索形式,它增量地构造数组中目标值的索引。像普通的二分搜索一样,元二分搜索需要O(log n)时间。

元二分搜索,也称为单边二分搜索,是二分搜索算法的一种变体,用于搜索有序列表或元素数组。该算法旨在减少在列表中搜索给定元素所需的比较次数。

元二分搜索的基本思想是从包含整个数组的大小为n的初始间隔开始。然后,该算法计算一个中间元素,就像二进制搜索一样,并将其与目标元素进行比较。如果找到目标元素,则搜索终止。如果中间元素大于目标元素,算法将新的间隔设置为前一个间隔的左半部分,如果中间元素小于目标元素,则新的间隔设置为前一个间隔的右半部分。然而,与二分搜索不同的是,元二分搜索做到了

相反,该算法使用启发式来确定下一个区间的大小。它计算中间元素的值与目标元素的值之间的差值,并将差值除以一个预定的常数,通常为2。然后将该结果用作新间隔的大小。该算法继续执行,直到找到目标元素或确定它不在列表中。

元二分搜索相对于二分搜索的优点是,在某些情况下,它可以执行更少的比较,特别是当目标元素接近列表的开头时。缺点是在其他情况下,该算法可能比二进制搜索执行更多的比较,特别是当目标元素接近列表的末尾时。因此,当列表的顺序与目标元素的分布一致时,元二进制搜索是最有效的。

下面是元二分搜索的伪代码:

function meta_binary_search(A, target):n = length(A)interval_size = nwhile interval_size > 0:index = min(n - 1, interval_size / 2)mid = A[index]if mid == target:return indexelif mid < target:interval_size = (n - index) / 2else:interval_size = index / 2return -1

示例:

Input: [-10, -5, 4, 6, 8, 10, 11], key_to_search = 10

Output: 5

Input: [-2, 10, 100, 250, 32315], key_to_search = -2

Output: 0

具体实现各不相同,但基本算法包括两部分:

  1. 计算存储最大数组下标需要多少位。

  1. 通过确定索引中的每个位应该设置为1还是0,递增地构造数组中目标值的索引。

实现方法

  • 存储表示变量lg中最大数组下标的比特数。

  • 使用lg在for循环中开始搜索。

  • 如果找到元素,则返回pos。

  • 否则,增量地构造一个索引以达到for循环中的目标值。

  • 如果元素找到则返回pos,否则返回-1。

下面是上述方法的C++实现:

// C++ implementation of above approach#include 
#include 
#include 
using namespace std;// Function to show the working of Meta binary search
int bsearch(vector A, int key_to_search)
{int n = (int)A.size();// Set number of bits to represent largest array indexint lg = log2(n-1)+1;//while ((1 << lg) < n - 1)//lg += 1;int pos = 0;for (int i = lg ; i >= 0; i--) {if (A[pos] == key_to_search)return pos;// Incrementally construct the// index of the target valueint new_pos = pos | (1 << i);// find the element in one// direction and update positionif ((new_pos < n) && (A[new_pos] <= key_to_search))pos = new_pos;}// if element found return pos otherwise -1return ((A[pos] == key_to_search) ? pos : -1);
}// Driver code
int main(void)
{vector A = { -2, 10, 100, 250, 32315 };cout << bsearch(A, 10) << endl;return 0;
}// This implementation was improved by Tanin

输出:

1

下面代码是java方式实现

//Java implementation of above approach
import java.util.Vector;
import com.google.common.math.BigIntegerMath;
import java.math.*;class GFG {// Function to show the working of Meta binary searchstatic int bsearch(Vector A, int key_to_search) {int n = (int) A.size();// Set number of bits to represent largest array indexint lg = BigIntegerMath.log2(BigInteger.valueOf(n-1),RoundingMode.UNNECESSARY) + 1;//while ((1 << lg) < n - 1) {// lg += 1;//}int pos = 0;for (int i = lg - 1; i >= 0; i--) {if (A.get(pos) == key_to_search) {return pos;}// Incrementally construct the// index of the target valueint new_pos = pos | (1 << i);// find the element in one// direction and update positionif ((new_pos < n) && (A.get(new_pos) <= key_to_search)) {pos = new_pos;}}// if element found return pos otherwise -1return ((A.get(pos) == key_to_search) ? pos : -1);}// Driver codestatic public void main(String[] args) {Vector A = new Vector();int[] arr = {-2, 10, 100, 250, 32315};for (int i = 0; i < arr.length; i++) {A.add(arr[i]);}System.out.println(bsearch(A, 10));}
}// This code is contributed by 29AjayKumar
// This implementation was improved by Tanin

时间复杂度:O(log n),其中n是给定数组的大小

辅助空间:O(1),因为我们没有使用任何额外的空间

上一篇:HelloWorld

下一篇:计算机网络复习重点

相关内容

热门资讯

“清新福建”展馆斩获2025中... 由文化和旅游部主办的2025中国国际旅游交易会12月21日下午在海南国际会议展览中心(海口)圆满落幕...
广安市文化和旅游市场整治工作专... 为深入贯彻落实全省文化和旅游市场整治工作会议精神,进一步规范文旅市场经营秩序,提升行业服务质量,12...
周一003 非洲杯:安哥拉VS... 公众号 【墨鱼足球分析】 获取每日精选二串方案 北京时间1月2日00:00,2025非洲杯F组次轮对...
玉树州建设国际生态旅游目的地导... 为发掘优秀讲解人才,助力国际生态旅游目的地建设,12月26日,玉树州首届“我当家乡推介官”——玉树州...
“日常餐酒” 藏惊喜,VDT ... 多数人对法国红酒的认知停留在 AOP,但 VDT(日常餐酒)里藏着不少性价比宝藏。2009 年法国葡...