定义:

  二分法检索的基本思想是设字典中的元素从小到大有序地存放在数组(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. 学习笔记-AngularJs(四)

    之前学习的事视图与模版,我们在控制器文件中直接定义一个数组,让其在模版文件中用ng-repeat指令构造一个迭代器,定义的数组http://t.cn/RUbL4rP如同以下: $scope.phone ...

  2. 002-linux——控制台的使用:

    1.桌面控制台: 2.字符控制台: .默认6个字符控制台. .独立运行 互不影响 .多用户  多任务 tty-控制台的使用: .开始进入的是图形图面:tty1 就是图形界面. .图形界面切换到字符界面 ...

  3. 遍历存储所有物体添加到列表中(使用GameObject.activeSelf进行判断)

    //存储菜单列表 List<GameObject> subMenu = new List<GameObject>(); //存储所有子菜单 public void StoreS ...

  4. mybatis实战教程

    参考:http://blog.csdn.net/techbirds_bao/article/details/9233599/

  5. JQuery button控制div或者section

    一.项目你需求 点击左边导航栏的某个按钮,右边内容栏显示出,相应的内容 效果如图   二.html与css.jQuery 1.div模式 <!DOCTYPE html PUBLIC " ...

  6. 每天CSS学习之!important

    通过在CSS属性后面跟上!important参数,就会让该属性的优先级变为最高,不管是在css文件,还是内联style中,就属后面跟了!important这个小弟的属性的优先级最高. 让我们来测试一下 ...

  7. vue-5-列表渲染

    一个数组的v-for<ul id="example-1"> <li v-for="item in items"> {{ item.mes ...

  8. BOM浏览器操作对象

    BOM定义 一.定时器 1) 定时器1:setInterval() 2)一次性定时器 二.offset.scroll.client

  9. jquery去重复 如何去除select控件重复的option

    #1.去除select控件重复的option <select id="companyId" onchange="getContract()" name=& ...

  10. Java流对象理解

    马士兵老师,曾在Java的学习过程中,将Java的流比作管道,认为很贴切,在此笔者也建议读者在学习过程中作类似比喻,形象化的学习 Java根据数据流向的不同分为输入流和输出流: Java根据处理数据类 ...