[图解算法] 二分查找Binary-Search——<递归与分治策略>
#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——<递归与分治策略>的更多相关文章
- [图解算法]线性时间选择Linear Select——<递归与分治策略>
#include <ctime> #include <iostream> using namespace std; template <class Type> vo ...
- STL之二分查找 (Binary search in STL)
STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...
- 【转】STL之二分查找 (Binary search in STL)
Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...
- 二分查找(binary search)
二分查找又叫折半查找,要查找的前提是检索结果位于已排序的列表中. 概念 在一个已排序的数组seq中,使用二分查找v,假如这个数组的范围是[low...high],我们要的v就在这个范围里.查找的方法是 ...
- LeetCode 704. 二分查找(Binary Search)
704. 二分查找 704. Binary Search 题目描述 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果 ...
- 数据结构-二分查找(Binary Search)
#include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...
- [Swift]LeetCode704. 二分查找 | Binary Search
Given a sorted (in ascending order) integer array nums of nelements and a target value, write a func ...
- 二分查找(非递归JAVA)
庞果网编程英雄会上做的一道题:二分查找(非递归),和大家分享一下: public class BinarySearchClass { public static int binary_search(i ...
- 递归与分治策略之循环赛日程表Java实现
递归与分治策略之循环赛日程表 一.问题描述 设有n=2^k个运动员要进行网球循环赛.现要设计一个满足以下要求的比赛日程表: (1)每个选手必须与其他n-1个选手各赛一次: (2)每个选手一天只能参赛一 ...
随机推荐
- [代码]--c#获取系统时间
public DateTime GetServerDataTime() { string sql = " select top 1 getdate() from sysobjects &qu ...
- BZOJ2303 APIO2011方格染色(并查集)
比较难想到的是将题目中的要求看做异或.那么有ai,j^ai+1,j^ai,j+1^ai+1,j+1=1.瞎化一化可以大胆猜想得到a1,1^a1,j^ai,1^ai,j=(i-1)*(j-1)& ...
- 1019C Sergey's problem(思维)
题意: 找出来一个点集S 使得S中的点不能互相通过一步到达 并且S中的点 可以在小于等于2的步数下到达所有的点 要父结点 不要子结点 这样就求出来一个点集S‘ 而S'中可能存在 v -> u ...
- 【模板】BM + CH(线性递推式的求解,常系数齐次线性递推)
这里所有的内容都将有关于一个线性递推: $f_{n} = \sum\limits_{i = 1}^{k} a_{i} * f_{n - i}$,其中$f_{0}, f_{1}, ... , f_{k ...
- 前端学习 -- Css -- 浮动
块元素在文档流中默认垂直排列,所以这个三个div自上至下依次排开,如果希望块元素在页面中水平排列,可以使块元素脱离文档流. 使用float来使元素浮动,从而脱离文档流 可选值: none,默认值,元素 ...
- 解题:NOI 2010 超级钢琴
题面 WC时候写的题,补一下 做法比较巧妙:记录每个位置和它当前对应区间的左右端点,做前缀和之后重载一下小于号,用优先队列+ST表维护当前最大值.这样贡献就是区间最大值和端点左边差分一下,可以O(1) ...
- oracle递归查询(查询条件ID下得所有子集)
一.CREATE TABLE TBL_TEST ( ID NUMBER, NAME VARCHAR2(100 BYTE), PID NUMBER ...
- JDK自带线程池介绍及使用环境
1.newFixedThreadPool创建一个指定工作线程数量的线程池.每当提交一个任务就创建一个工作线程,如果工作线程数量达到线程池初始的最大数,则将提交的任务存入到池队列中. 2.newCach ...
- 螺旋队列和hiho1525逃离迷宫3
我是真调不出错误了! hiho1525逃离迷宫3 #include <stdio.h> #include <stdlib.h> #include <math.h> ...
- HDU 6156 数位dp
Palindrome Function Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Ot ...