查找算法之二分法搜索算法
创始人
2025-05-29 20:44:36
0

二分搜索是一种在已排序的数组中反复将搜索间隔一分为二的搜索算法。二分搜索的思想是利用数组被排序的信息,将时间复杂度降低到O(Log n)。

二叉搜索算法:执行二叉搜索的基本步骤是:

  1. 按升序对数组排序。

  1. 将低索引设置为数组的第一个元素,高索引设置为数组的最后一个元素。

  1. 将中间指数设置为低指数和高指数的平均值。

  1. 如果位于中间下标的元素是目标元素,则返回中间下标。

  1. 如果目标元素小于中间索引处的元素,则将高索引设置为中间索引- 1。

  1. 如果目标元素大于中间下标处的元素,则将低下标设置为中间下标+ 1。

  1. 重复步骤3-6,直到找到该元素或很明显该元素不存在

二分搜索算法可以通过以下两种方式实现:迭代法和递归法

  1. 迭代法

binarySearch(arr, x, low, high)

repeat till low = high

mid = (low + high)/2

if (x == arr[mid])

return mid

else if (x > arr[mid]) // x is on the right side

low = mid + 1

else // x is on the left side

high = mid - 1

递归方法(递归方法采用分而治之的方法)

binarySearch(arr, x, low, high)

if low > high

return False

else

mid = (low + high) / 2

if x == arr[mid]

return mid

else if x > arr[mid] // x is on the right side

return binarySearch(arr, x, mid + 1, high)

else // x is on the left side

return binarySearch(arr, x, low, mid - 1)

二叉搜索算法图说明:

逐步二分搜索算法:我们基本上忽略一半的元素后,只是一个比较。

比较x和中间的元素。

如果x与中间元素匹配,则返回中间索引。

如果x大于中间元素,则x只能位于中间元素之后的右半子数组中。对右半部分进行递归。

Else (x更小)对左半部分重复。

以下代码为二分查找的递归C/C++实现:

// C++ program to implement recursive Binary Search
#include 
using namespace std;// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{if (r >= l) {int mid = l + (r - l) / 2;// If the element is present at the middle// itselfif (arr[mid] == x)return mid;// If element is smaller than mid, then// it can only be present in left subarrayif (arr[mid] > x)return binarySearch(arr, l, mid - 1, x);// Else the element can only be present// in right subarrayreturn binarySearch(arr, mid + 1, r, x);}// We reach here when element is not// present in arrayreturn -1;
}int main(void)
{int arr[] = { 2, 3, 4, 10, 40 };int x = 10;int n = sizeof(arr) / sizeof(arr[0]);int result = binarySearch(arr, 0, n - 1, x);(result == -1)? cout << "Element is not present in array": cout << "Element is present at index " << result;return 0;
}

输出

Element is present at index 3

二叉搜索的另一种迭代方法

#include 
#include 
using namespace std;int binarySearch(vector v, int To_Find)
{int lo = 0, hi = v.size() - 1;int mid;// This below check covers all cases , so need to check// for mid=lo-(hi-lo)/2while (hi - lo > 1) {int mid = (hi + lo) / 2;if (v[mid] < To_Find) {lo = mid + 1;}else {hi = mid;}}if (v[lo] == To_Find) {cout << "Found"<< " At Index " << lo << endl;}else if (v[hi] == To_Find) {cout << "Found"<< " At Index " << hi << endl;}else {cout << "Not Found" << endl;}
}int main()
{vector v = { 1, 3, 4, 5, 6 };int To_Find = 1;binarySearch(v, To_Find);To_Find = 6;binarySearch(v, To_Find);To_Find = 10;binarySearch(v, To_Find);return 0;
}

输出

Found At Index 0

Found At Index 4

Not Found

二叉搜索的迭代实现

// C++ program to implement iterative Binary Search
#include 
using namespace std;// A iterative binary search function. It returns
// location of x in given array arr[l..r] if present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{while (l <= r) {int m = l + (r - l) / 2;// Check if x is present at midif (arr[m] == x)return m;// If x greater, ignore left halfif (arr[m] < x)l = m + 1;// If x is smaller, ignore right halfelser = m - 1;}// if we reach here, then element was// not presentreturn -1;
}int main(void)
{int arr[] = { 2, 3, 4, 10, 40 };int x = 10;int n = sizeof(arr) / sizeof(arr[0]);int result = binarySearch(arr, 0, n - 1, x);(result == -1)? cout << "Element is not present in array": cout << "Element is present at index " << result;return 0;
}

输出

Element is present at index 3

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...