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

迭代版本算法:

//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. Find the Duplicate Number 解答

    Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...

  2. SQL server 中 COUNT DISTINCT 函数

    目的:统计去重后表中所有项总和. 直观想法: SELECT COUNT(DISTINCT *) FROM [tablename] 结果是:语法错误. 事实上,我们可以一同使用 DISTINCT 和 C ...

  3. C++里消除Wunused

    编译程序时,有一大堆警告总是不爽的.别人的代码也就忍了,不好去改.自己的可没法忍.看看C++里怎么消除Wunused警告. 先来看下面的程序: #include <iostream> in ...

  4. pyqt QTimer,QThread例子学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from PyQ ...

  5. c++之 scanf 接收用户输入内容

    该代码全部在Visual Studio 2015中编写,有关VS2015的安装后期在写相关的博客 scanf 介绍 在程序开发中,有时候需要通过获取用户输入的数据,进行对应的操作,scanf就是用来接 ...

  6. EasyUI 两个日期比较

    两个日期进行比较,后一个日期不能早于晚一个日期.需要自己去扩展validatebox的方法 1.对比2个密码是否相同 $.extend($.fn.validatebox.defaults.rules, ...

  7. Centos 7 python升级(2.7.5-》2.7.11)

    1.安装升级GCC yum install gcc* openssl openssl-devel ncurses-devel.x86_64  bzip2-devel sqlite-devel pyth ...

  8. MySql命令——表相关

    auto_increment //自动增长 primary key(id) //指定主键 select last_insert_id();//获得添加列的主键值 create table produc ...

  9. javascript 之 this 用法

    参考视频:http://www.imooc.com/video/6430 JavaScript中的this比较灵活,也是让很多初学者摸不到头脑,那么根据在不同的环境下,在同一个函数,不同的调用方式下, ...

  10. jquery中的 ajax 以及map遍历

    1.语法 $.ajax{ type:'get',//类型 有get post url:'',//路径 data:{name:$('#ma').val(),nameq:$('#maq').val()}, ...