Binary Search--二分查找

  采用二分法查找时,数据需是排好序的。 基本思想:假设数据是按升序排序的,对于给定值x,从序列的中间位置开始比较,如果当前位置值等于x,则查找成功;若x小于当前位置值,则在数列的前半段 中查找;若x大于当前位置值则在数列的后半段中继续查找,直到找到为止。

  二分法查找在针对大量有序排列的情况下发挥出很优越的效率,其时间复杂度O(lgN)。

  

  代码:

 /*
Author:Mengmeng
Time:2016-6-27 23:33:49
Description:
采用二分法查找时,数据需是排好序的。
基本思想:假设数据是按升序排序的,对于给定值x,从序列的中间位置开始比较,
如果当前位置值等于x,则查找成功;若x小于当前位置值,则在数列的前半段 中查找;
若x大于当前位置值则在数列的后半段中继续查找,直到找到为止。
*/
#include <iostream>
using namespace std; int BinarySearch(int data[],int min,int max,int dest,bool UpOrDown)
{
int mid = ;
if (min > max)//递归结束条件
{
cout << "找不到" << dest << "!"<<endl;
return -;
}
mid = (max + min) / ;
if (dest == data[mid])//递归结束条件
return mid;
if (UpOrDown == true)//升序排列
{
if (dest < data[mid])//递归条件
return BinarySearch(data, min, mid - , dest, true);
else
return BinarySearch(data, mid + , max, dest, true);
}
else//降序排列
{
if (dest < data[mid])
return BinarySearch(data, mid + , max, dest, false);
else
return BinarySearch(data, min, mid - , dest, false);
} }
int main(void)
{
int data1[] = { , , ,,,,,,, };
int data2[] = { , , , , , , , , , };
#if 0
cout << "请参照下列数字:" << endl;
cout<< "{";
int len = sizeof(data1) / sizeof(int);
for (int i = ; i < len; i++)
cout << data1[i] << " ";
cout << "}" << endl;
cout << "输入你要查找的目标:" << endl;
int dest;
cin >> dest;
cout <<"----------------------------------------"<< endl;
int index = BinarySearch(data1, , len - , dest,true);
#endif
#if 1
cout << "请参照下列数字:" << endl;
cout << "{";
int len = sizeof(data2) / sizeof(int);
for (int i = ; i < len; i++)
cout << data2[i] << " ";
cout << "}" << endl;
cout << "输入你要查找的目标:" << endl;
int dest;
cin >> dest;
cout << "----------------------------------------" << endl;
int index = BinarySearch(data2, , len - , dest, false);
#endif
if (index!=-)
cout << dest << "在数组中的位置为第" << index << "个" << endl;
return ; }

  运行结果:

  (1)升序的情况:

    

  

  (2)降序的情况:

    

  

Binary Search--二分查找的更多相关文章

  1. [01]Binary Search二分查找

    Binary Search二分查找 作用:二分查找适用于有序的的数组或列表中,如果列表及数组中有n个元素,通过二分查找查询某一元素的位置需要的步骤是log2(n)(注:该log的底数是2) 1.Pyt ...

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

    题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime:  0 ms, faster than 100 % Memory Usage ...

  3. lintcode:Binary Search 二分查找

    题目: 二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 ...

  4. STL模板整理 Binary search(二分查找)

    前言: 之前做题二分都是手动二分造轮子,用起来总是差强人意,后来看到STL才发现前辈们早就把轮子造好了,不得不说比自己手动实现好多了. 常用操作 1.头文件 #include <algorith ...

  5. 【算法模板】Binary Search 二分查找

    模板:(通用模板,推荐) 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. ...

  6. Leetcode704.Binary Search二分查找

    给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...

  7. [LeetCode] Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  8. 501. Find Mode in Binary Search Tree查找BST中的众数

    [抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently oc ...

  9. Codeforces Round #678 (Div. 2) C. Binary Search (二分,组合数)

    题意:有长度\(n\)的序列,让你构造序列,使得二分查找能在\(pos\)位置找到值\(x\).问最多能构造出多少种排列? 题解:题目给出的\(pos\)是固定的,所以我们可以根据图中所给的代码来进行 ...

  10. LeetCode Binary Search All In One

    LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...

随机推荐

  1. HYSBZ 4197 寿司晚宴

    Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了 n−1 种不同 ...

  2. java 中LinkedList的学习

    Java中,所有链表实际上都是双向链表的,即每个结点还存放在着指向前驱结点的引用. LinkedList中的contains方法检测某个元素是否出现在链表中. LinkedList类提供了一个用来访问 ...

  3. Nutch的配置以及动态网站的抓取

    http://blog.csdn.net/jimanyu/article/details/5619949 一:配置Nutch: 1.解压缩的nutch后,以抓取http://www.163.com/为 ...

  4. php中单例模式的解析说明

    <?php //单例模式 class Dbconn{ private static $_instance=null; protected static $_counter=0; protecte ...

  5. Idea 添加lib文件夹,并添加至项目Libary

    在WEB-INF文件夹下新建lib文件夹,在lib文件夹上右键选择Add as Libary...,然后填写library名称,选择作用级别,选择作用项目,OK 注意:lib文件夹下需要有jar包后才 ...

  6. [LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal

    前中后遍历 递归版 /* Recursive solution */ class Solution { public: vector<int> preorderTraversal(Tree ...

  7. Center os FTP配置

    原文:http://www.aicoffees.com/itshare/412261137.html

  8. WPF 检测输入状态

    [DllImport("user32.dll")] static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); pub ...

  9. SQL Server中的联合主键、聚集索引、非聚集索引、mysql 联合索引

    我们都知道在一个表中当需要2列以上才能确定记录的唯一性的时候,就需要用到联合主键,当建立联合主键以后,在查询数据的时候性能就会有很大的提升,不过并不是对联合主键的任何列单独查询的时候性能都会提升,但我 ...

  10. ios框架中UIResponder的职责链设计模式应用

    今天有空,就把UIResponder的职责链图上传一下 如果不理解职责链设计模式的朋友,请参考:http://www.cnblogs.com/langtianya/p/4060941.html