#include"iostream.h"

 int BinarySearch(int a[],int left,int right,const int& x)
{
if(left<right)
{
int middle = (left+right)/;
if(x==a[middle]) return middle;
if(x>a[middle]) return BinarySearch(a,middle+,right,x);
else return BinarySearch(a,left,middle-,x);
}
} void main(){
int n,x,i,a[];
cout<<"input the length of a[]"<<endl;
cin>>n;
cout<<"input array a[]"<<endl;
for(i=;i<n;i++) cin>>a[i];
cout<<"input the num u want to find"<<endl;
cin>>x;
cout<<BinarySearch(a,,n-,x)+<<endl;
}

[图解+例子]

前提:有序数组。

一、建立数组

(共10个数)

二、传参

int a[] 数组

int left,int right 当前查找范围限定(left=0;right=n-1;10个数即n=10;left=0;right=9

const int& x 待查找数值(假如查找27

三、取中间值middle判断

如果27==7 返回当前middle值(0+9)/2=4

如果27>7查找右半段

if (x>a[middle]) return BinarySearch(a,middle+1,right,x);  //设定右半段范围(right值不变middle+1)

如果27<7查找左半段

else return BinarySearch(a,left,middle-1,x);//设定左半段范围(left值不变middle-1)

当前查找右半段即  left=5;right=9

如果27==25 返回当前middle值(5+9)/2=7

……

……

如此继续递归调用BinarySearch函数

直到middle=(8+9)/2=8时 ;27==27;return middle

[特例]

如果要找元素在边上的话,如36,那么下一次就是left=9;right=9;自然middle也等于9。

[总结]

子问题:范围内取中值;递归不断缩小确定范围。

[图解算法] 二分查找Binary-Search——<递归与分治策略>的更多相关文章

  1. [图解算法]线性时间选择Linear Select——<递归与分治策略>

    #include <ctime> #include <iostream> using namespace std; template <class Type> vo ...

  2. STL之二分查找 (Binary search in STL)

    STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...

  3. 【转】STL之二分查找 (Binary search in STL)

    Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...

  4. 二分查找(binary search)

    二分查找又叫折半查找,要查找的前提是检索结果位于已排序的列表中. 概念 在一个已排序的数组seq中,使用二分查找v,假如这个数组的范围是[low...high],我们要的v就在这个范围里.查找的方法是 ...

  5. LeetCode 704. 二分查找(Binary Search)

    704. 二分查找 704. Binary Search 题目描述 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果 ...

  6. 数据结构-二分查找(Binary Search)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...

  7. [Swift]LeetCode704. 二分查找 | Binary Search

    Given a sorted (in ascending order) integer array nums of nelements and a target value, write a func ...

  8. 二分查找(非递归JAVA)

    庞果网编程英雄会上做的一道题:二分查找(非递归),和大家分享一下: public class BinarySearchClass { public static int binary_search(i ...

  9. 递归与分治策略之循环赛日程表Java实现

    递归与分治策略之循环赛日程表 一.问题描述 设有n=2^k个运动员要进行网球循环赛.现要设计一个满足以下要求的比赛日程表: (1)每个选手必须与其他n-1个选手各赛一次: (2)每个选手一天只能参赛一 ...

随机推荐

  1. 一本通1646GT 考试

    1646:GT 考试 时间限制: 1000 ms         内存限制: 524288 KB [题目描述] 阿申准备报名参加 GT 考试,准考证号为 n 位数 X1X2⋯Xn(0≤Xi≤9),他不 ...

  2. 选择提供器 - 选择监听器(selection provider-selection listener)模式

             

  3. gcc 与 glibc 的关系 glibc版本查看

    glibc是什么,以及与gcc的关系?glibc是gnu发布的libc库,也即c运行库.glibc是linux 系统中最底层的api(应用程序开发接口),几乎其它任何的运行库都会倚赖于glibc.gl ...

  4. Problem B: 专家系统 解题报告

    Problem B: 专家系统 Description 一个专家系统是指,你雇佣了\(n\)个专家,他们每个人会做出一个结果,然后你从中选取较多的专家的结果组合而成最终的结果.专家系统广泛应用于传统机 ...

  5. 【bzoj2434】 Noi2011—阿狸的打字机

    http://www.lydsy.com/JudgeOnline/problem.php?id=2434 (题目链接) 题意 给出一个字符串,$P$表示输出,$B$表示退格.$m$组询问$(x,y)$ ...

  6. ASP.NET MVC项目框架快速搭建实战

    MVC项目搭建笔记---- 项目框架采用ASP.NET MVC+Entity Framwork+Spring.Net等技术搭建,采用”Domain Model as View Model“的MVC开发 ...

  7. LRN

    转自https://blog.csdn.net/u011204487/article/details/76026537 LRN全称为Local Response Normalization,即局部响应 ...

  8. 各种奇妙的hack

    Android Selector Hacks WebKit .selector:not(*:root) {} Chrome * Safari * Opera ≥ 14 Android * # Java ...

  9. SOCKET中send和recv函数工作原理与注意点

    https://blog.csdn.net/rankun1/article/details/50488989

  10. rsync更改端口后的同步办法

    rsync有两种常用的认证方式,一种为rsync-daemon方式,另外一种则是ssh. 在一些场合,使用rsync-daemon方式会比较缺乏灵活性,ssh方式则成为首选.但是今天实际操作的时候发现 ...