定义:

  二分法检索的基本思想是设字典中的元素从小到大有序地存放在数组(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. Win10系列:WinJS库控件

    在介绍了如何使用标准的HTML控件以及WinJS库中提供的新控件之后,下面来着重介绍WinJS库中几种常用的控件. (1)ListView控件 在开发Windows应用商店应用时可以使用ListVie ...

  2. 每天CSS学习之text-decoration

    text-decoration是CSS的一个属性,其作用是给文本装饰上划线.中间线.下划线或不装饰.其值如下所示: 1.none:不装饰任何线.该值是默认值.如下所示: p{ text-decorat ...

  3. 上传本地代码到GitHub上

    由于经常忘记Git的相关代码,百度多了自然不耐烦,干脆自己写个简单的博客记录一下代码及流程了...... 1.在GitHub上新建一个仓库: 2.创建完后在仓库左上角的ssh上copy一下地址: 3. ...

  4. HTML--思维导图

    HTML--思维导图

  5. 对编译特性(* ASYNC_REG = “TRUE” *)的理解

    (*ASYNC_REG = "TRUE"*)命令用于声明寄存器能够接收相对于时钟源的异步数据,或者说寄存器是一个同步链路上正在同步的寄存器.这条命令可以放在任何寄存器上,除了设置它 ...

  6. tomcat的安装及配置

    1.首先进tomcat官网下载zip压缩文件:http://tomcat.apache.org/download-90.cgi 2.解压缩到指定文件压(后面配置环境变量会用到) 3.配置环境变量 4. ...

  7. spingmvc 访问静态文件,比如css,img等

    这里我来引用一段别人的原话 url-pattern有5种配置模式: (1)/xxx:完全匹配/xxx的路径 (2)/xxx/*:匹配以/xxx开头的路径,请求中必须包含xxx. (3)/*:匹配/下的 ...

  8. L323 英语有必要学语法吗

    The Agony and Ecstasy of Grammar “Underline a relative clause.” This challenge would give a lot of a ...

  9. 如何用UltraEdit查看并修改Oracle导出的dump文件的字符集

    如何查询dmp文件的字符集 用oracle的exp工具导出的dmp文件也包含了字符集信息,dmp文件的第2和第3个字节记录了dmp文件的字符集.如果dmp文件不大,比如只有几M或几十M,可以用Ultr ...

  10. Flask源码阅读-第二篇(flask\__init__.py)

    源码: # -*- coding: utf-8 -*-""" flask ~~~~~ A microframework based on Werkzeug. It's e ...