定义:

  二分法检索的基本思想是设字典中的元素从小到大有序地存放在数组(array)中。首先将给定值key与字典中间位置上元素的关键码(key)比较,如果相等,则检索成功;否则,若key小,则在字典前半部分中继续进行二分法检索;若key大,则在字典后半部分中继续进行二分法检索。这样,经过一次比较就缩小一半的检索区间,如此进行下去,直到检索成功或检索失败。偶数个取中间2个其中任何一个作为中间元素。二分法检索是一种效率较高的检索方法,要求字典在顺序表中按关键码排序。

例题:

大意为:

  给定一个双调数组(数组中数字先递增后递减),例如1,3,5,7,6,4,2,0,-2,-4,-6,-10。另外给定一个数字X。

  设计一个算法,找出X是否在数组中。当此数组的数字总量n为很大的一个数值时,寻找X时,程序最多运行大约2lgN次。lg为底数为2的对数,lg4=2;lg8=3。

解答:

  解题思路:首先,直接遍历是肯定不行的,因为当n很大时,如果X是数组的最后一个数字,则程序要运行N次才能找到。例如:

   int x;
//如果x==RawArray[RawArray.Num()-1],则这个要遍历所有数字,运行了N次
for (int i=;i<RawArray.Num();++i)
{
if (RawArray[i] == x) return;
}

  然后,解题方法为:利用二分法检索来检索。先用二分法检索找到递增区域和递减区域的共同数字(上述例子中的7);然后分别在递增区域和递减区域中用二分法检索来寻找X。

  具体实现:

.h:

UCLASS()
class ALGORITHM_API AAnalysisExerciseOne : public AActor
{
GENERATED_BODY() public:
// Sets default values for this actor's properties
AAnalysisExerciseOne(); //在数组中寻找X,如果找到,返回True;反之,返回false;
bool FindIntX(TArray<int> TargetArray, int X);
//二叉法找Int
bool XInArray(TArray<int> TargetArray, int LeftIndex, int RightIndex, int X, bool IncreaseSide); protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override; public:
// Called every frame
virtual void Tick(float DeltaTime) override; private: TArray<int> RawArray; }; .cpp: // Sets default values
AAnalysisExerciseOne::AAnalysisExerciseOne()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
} //在数组中寻找X,如果找到,返回True;反之,返回false;
bool AAnalysisExerciseOne::FindIntX(TArray<int> TargetArray, int X)
{
//利用二叉法,寻找递增数组和递减数组的共用元素
//左右两个点
int Left();
int Right(TargetArray.Num());
//这个是要找的元素的index
int TargetInt();
//开始寻找
while (Left <= Right)
{
//找中间点,如果有小数点,舍弃,取整
int Mid = Left + (Right - Left) / ;
if (TargetArray[Mid - ] < TargetArray[Mid] && TargetArray[Mid]>TargetArray[Mid + ])
{
//找到了,结束While
TargetInt = Mid;
break;
}
else if (TargetArray[Mid - ] < TargetArray[Mid]) { Left = Mid + ; }
else if (TargetArray[Mid] > TargetArray[Mid + ]) { Right = Mid - ; }
//在双调数组中,TargetArray[Mid - 1] > TargetArray[Mid] && TargetArray[Mid] < TargetArray[Mid + 1]的情况不存在
else break;
}
//等于0说明没找到,出问题了,返回
if (TargetInt == ) return false;
//X比数组中所有数字都大,X不在数组中
if (X > TargetArray[TargetInt]) return false;
if (X == TargetArray[TargetInt]) return true;
//先在增长数组中找
if (XInArray(TargetArray, , TargetInt - , X, true))
{
UKismetSystemLibrary::PrintString(this, "Find in increase side!");
return true;
}
//如果在增长数组中找不到,则去减少数组中找
else
{
if (XInArray(TargetArray, TargetInt + , TargetArray.Num() - , X, false))
{
UKismetSystemLibrary::PrintString(this, "Find in Decrease side!");
return true;
}
//都找不到,说明没有
else
{
UKismetSystemLibrary::PrintString(this, "Don't Find it!");
return false;
}
}
} bool AAnalysisExerciseOne::XInArray(TArray<int> TargetArray, int LeftIndex, int RightIndex, int X, bool IncreaseSide)
{
int Left(LeftIndex);
int Right(RightIndex);
//开始寻找
if (IncreaseSide)
{
//在递增区域中寻找
while (Left <= Right)
{
//找中间点,如果有小数点,舍弃,取整
int Mid = Left + (Right - Left) / ;
if (X < TargetArray[Mid]) { Right = Mid - ; }
else if (X > TargetArray[Mid]) { Left = Mid + ; }
else return true;
}
return false;
}
//在递减区域中寻找
else
{
while (Left <= Right)
{
//找中间点,如果有小数点,舍弃,取整
int Mid = Left + (Right - Left) / ;
if (X > TargetArray[Mid]) { Right = Mid - ; }
else if (X < TargetArray[Mid]) { Left = Mid + ; }
else return true;
}
return false;
}
} // Called when the game starts or when spawned
void AAnalysisExerciseOne::BeginPlay()
{
Super::BeginPlay();
   //测试
//给定一个初始数组
RawArray.Add();
RawArray.Add();
RawArray.Add();
RawArray.Add();
RawArray.Add();
RawArray.Add();
RawArray.Add();
RawArray.Add();
RawArray.Add(-);
RawArray.Add(-);
RawArray.Add(-);
RawArray.Add(-);
if (FindIntX(RawArray, -))
{
UKismetSystemLibrary::PrintString(this, "Find it!");
}
else
{
UKismetSystemLibrary::PrintString(this, "Don't Find it!");
} } // Called every frame
void AAnalysisExerciseOne::Tick(float DeltaTime)
{
Super::Tick(DeltaTime); }

二分法检索(binary search)(又名二进制搜索)的更多相关文章

  1. 二分法查找(Binary Search)

    --摘要:二分法的介绍已经很多了,但并不直观,因此此文诞生,希望批评指正. 二分查找是在有序数组中查找一个元素的算法,通过比较目标元素与数组中间元素来查找,如果目标值是中间元素则将返回中间元素位置. ...

  2. Leetcode之二分法专题-704. 二分查找(Binary Search)

    Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 t ...

  3. Binary Search 的递归与迭代实现及STL中的搜索相关内容

    与排序算法不同,搜索算法是比较统一的,常用的搜索除hash外仅有两种,包括不需要排序的线性搜索和需要排序的binary search. 首先介绍一下binary search,其原理很直接,不断地选取 ...

  4. [Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  5. [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  6. [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  7. Binary Search 二分法方法总结

    Binary Search 二分法方法总结 code教你做人:二分法核心思想是把一个大的问题拆成若干个小问题,最重要的是去掉一半或者选择一半. 二分法模板: public int BinarySear ...

  8. 270. Closest Binary Search Tree Value 二叉搜索树中,距离目标值最近的节点

    [抄题]: Given a non-empty binary search tree and a target value, find the value in the BST that is clo ...

  9. [Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

随机推荐

  1. vs2015 出现Lc.exe 已退出,代码为-1的问题,如何解决

    今天在代码运行时,出现lc.exe已退出,代码为-1 的问题

  2. laravel基于Bootstrap的成功和失败的提示信息和验证提示信息

    message.blade.php <!-- 成功提示框 --> @if(Session::has("success")) <div class="al ...

  3. 五. Python基础(5)--语法

    五. Python基础(5)--语法 1 ● break结束的是它所在的循环体, continue是让它所在的循环体继续循环 # 打印: 1 10 2 10 3 10 4 10 5 10 6 10 7 ...

  4. capjoint conversations with Chenweiwen

    This event is quite small for teleseismic stations, which means it will be more strongly affected by ...

  5. quartz自定义线程数

    1.加载包 2.添加quartz.propertes 3.编写自己的任务类 4.添加自动任务配置 5.通过 quartzProperties 配置连接池 1.加载包 <dependency> ...

  6. HTTPS加密原理(转)

    Header HTTP.HTTPS在我们日常开发中是经常会接触到的. 我们也都知道,一般 Android 应用开发,在请求 API 网络接口的时候,很多使用的都是 HTTP 协议:使用浏览器打开网页, ...

  7. php优秀框架codeigniter学习系列——CI_URI类学习

    这篇文章主要介绍CI核心框架工具类CI_URI. 该类主要用来解析uri和决定路由的.关于URI和URL的关系请参考这位朋友的文章.简单来说URI是唯一定位的资源,URL是唯一资源的一个网络可能访问路 ...

  8. js 自执行匿名函数(转载)

    自执行匿名函数: 常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命 ...

  9. IO-MYSQL的理解

    数据库IO简介   IO有四种类型:连续读,随机读,随机写和连续写,连续读写的IO size通常比较大(128KB-1MB),主要衡量吞吐量,而随机读写的IO size比较小(小于8KB),主要衡量I ...

  10. python day02作业