当我们在字典中查找某个单的时候,一般我们会翻到一个大致的位置(假设吧,翻到中间位置),开始查找。如果翻到的正好有我们要的词,那运气好,查找结束。如果我们要找的词还在这个位置的前面,那我们对前面的这一半词典继续搜索,翻到某个位置(假设是四分之一的位置)等等。这个二分搜索的工作原理一样。相应的算法就叫做二进制搜索算法。

迭代版本算法:

//iterative binary search which returns index of element
int binarySearchIterative(int arr[], int size, int data)
{
int low = 0;
int high = size-1;
int mid; while(low<=high)
{
mid = low + (high-low)/2; //To avoid overflow by (low + high) if(arr[mid] == data)
return mid;
else
{
if(arr[mid] < data)
low = mid + 1; // search in right half
else
high = mid - 1; // search in left half
}
} return -1;
}

递归版本算法:

//recursive binary search which returns index of element
int binarySearchRecursive(int arr[], int low, int high, int data)
{
if(low<=high)
{
int mid = low + (high-low)/2; // To avoid overflow if(arr[mid] == data)
return mid;
else
{
if(arr[mid] < data)
//search in right half.
return binarySearchRecursive(arr, mid+1, high, data);
else
//search in left half.
return binarySearchRecursive(arr, low, mid-1, data);
}
}
return -1;
}

二分搜索(Binary Search)的更多相关文章

  1. 二分搜索 - Binary Search

    二分搜索是一种在有序数组中寻找目标值的经典方法,也就是说使用前提是『有序数组』.非常简单的题中『有序』特征非常明显,但更多时候可能需要我们自己去构造『有序数组』.下面我们从最基本的二分搜索开始逐步深入 ...

  2. 《算法导论》习题2.3-5 二分搜索 Binary Search

    地球人都知道“二分查找”,方法也非常简单,但是你能不能在10分钟内写出一个没有bug的程序呢? 知易行难,自己动手写一下试一试吧. public class BinarySearch { public ...

  3. 数据结构与算法--二分搜索(binary search)

    前言 之前面试准备秋招,重新翻起了<编程之美>.在第三章节看到了一道关于二分搜索的讨论,觉得有许多细节是自己之前也没怎么特别注意地方,比如二分搜索的初始条件,转化.终止条件之类的. 问题 ...

  4. [LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  5. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  6. 72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)

    题目描述: 一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5 例如: 2,8 - ...

  7. Algo: Binary search

    二分查找的基本写法: #include <vector> #include <iostream> int binarySearch(std::vector<int> ...

  8. LeetCode & Binary Search 解题模版

    LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...

  9. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

随机推荐

  1. 杭电2059(dp)

    龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. Spiral Matrix 解答

    Question Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ...

  3. 关于链表的一些重要操作(Important operations on a Linked List)

    上篇博文中讨论了链表的一些基本操作: 链表的基本操作(Basic Operations on a Linked List) 然而,为创建一个多功能的链表,在深度学习之前我们还需要了解更多的链表操作. ...

  4. 基于公网smtp协议实现邮件服务器

    刚开始做邮件服务器开发,一切都是茫然的.在书上网上都很难找到一套完整的邮件服务器开发教程.在个人的摸索中碰到了很多蛋疼得问题.现终于完成了,将我的开发经验分享给大家. 开发环境:vs2012 mfc ...

  5. LINQ to SQL 增,删,改

    添加   InsertOnSubmit(单个对象)  或  InsertAllOnSubmit(集合) 删除   DeleteOnSubmit (单个对象)             DeleteAll ...

  6. [Protractor] Running tests on multiple browsers

    Testing your AngularJS application on multiple browsers is important, and Protractor offers this abi ...

  7. [core java学习笔记][第十章部署应用程序]

    第10章 部署应用程序和applet jar文件 Java Web Start 10.1 jar文件 jar文件就是一个压缩了类,图像和声音的ZIP压缩文件 创建一个新的JAR文件应该使用的常见命令格 ...

  8. [服务器运维][Minecraft服务器搭建]

    参考资料: http://neekey.net/2016/02/01/%E5%A6%82%E4%BD%95%E7%94%A8%E9%98%BF%E9%87%8C%E4%BA%91ecs%E6%90%A ...

  9. 关于arm-linux-gcc的安装与配置

    在嵌入式开发中我们经常会用到arm-linux-gcc来编译我们的应用程序.作为arm-linux-gcc的入门,我们先看看如何安装arm-linux-gcc. 安装arm-linux-gcc还是比较 ...

  10. 初识Treap

    Treap,简单的来说就是Tree+Heap,是一颗平衡树,每个节点有两个信息:1.key:当前节点的关键字 :2.fix:当前节点优先级.key满足二叉排序数的性质,即左儿子都比当前节点小,右儿子都 ...