二分查找代码:

//============================================================================
// Name : BinarySearch.cpp
// Author : Danny
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
using namespace std; int binarySearch(int a[], int left, int right, int k) {
if (left > right)
return -;
int mid = (left + right) / ;
if (a[mid] == k) {
return mid;
} else if (a[mid] > k) {
return binarySearch(a, left, mid - , k);
} else {
return binarySearch(a, mid + , right, k);
}
} int main() {
int a[] = { , , , , };
int index = binarySearch(a, , , );
cout << index << endl;
return ;
}

Binary Search Algorithm的更多相关文章

  1. [Algorithms] Binary Search Algorithm using TypeScript

    (binary search trees) which form the basis of modern databases and immutable data structures. Binary ...

  2. 【437】Binary search algorithm,二分搜索算法

    Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...

  3. js binary search algorithm

    js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...

  4. [Math] Beating the binary search algorithm – interpolation search, galloping search

    From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...

  5. [Algorithm] Beating the Binary Search algorithm – Interpolation Search, Galloping Search

    From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...

  6. [UCSD白板题] Binary Search

    Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...

  7. Foundation: Binary Search

    /* Binary search. * * Implementation history: * 2013-10-5, Mars Fu, first version. */ /* [Binary Sea ...

  8. What's binary search?

    Binary Search: Search a sorted array by repeatedly  dividing the search interval in half. Begin with ...

  9. Binary Search - Jump on the Stones

    Binary Search algorithm. Wikipedia definition: In computer science, binary search, also known as hal ...

随机推荐

  1. OOM框架AutoMapper基本使用(1)

    OOM顾名思义,Object-Object-Mapping实体间相互转换,AutoMapper也是个老生常谈了,其意义在于帮助你无需手动的转换简单而又麻烦的实体间关系,比如ViewModel和enti ...

  2. C/C++(C++返回对象与应用区别,类成员的存储)

    返回对象与应用区别: 拷贝构造器发生的时机: 1.构造新对象 A a, A b = a; 2.传参或返回对象 对于普通变量来说,传引用效果不是很明显,对于类对象而言,传对象效果很高. 传引用等价于扩大 ...

  3. Python之路:画空心矩形

    1.if语句中当要判断多个语句成立才能执行语句时候,可以用or(或,只要其中一个成立就可以执行下一条语句)或者and(只要有其中一个不成立就不会执行下一条语句) 2.在Python中range函数会产 ...

  4. R学习:《机器学习与数据科学基于R的统计学习方法》中文PDF+代码

    当前,机器学习和数据科学都是很重要和热门的相关学科,需要深入地研究学习才能精通. <机器学习与数据科学基于R的统计学习方法>试图指导读者掌握如何完成涉及机器学习的数据科学项目.为数据科学家 ...

  5. Centos7最小化安装后再安装图形界面

    1. yum -y groupinstall "X Window System" 2. yum -y groupinstall "GNOME Desktop"  ...

  6. 为线程绑定CPU

    // learn gcc atomic variable #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> ...

  7. Android数据库高手秘籍(三)——使用LitePal升级表

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/39151617 在上一篇文章中,我们学习了LitePal的基本使用方法,体验了使用框 ...

  8. JAVA多态学习1

    多态–概念 所谓多态.就是指一个引用(类型)在不同情况下的多种状态. 也能够理解成:多态是指通过指向父类的指针,来调用在不同子类中实现的方法. 实现多态有两种方式:1.继承.2.接口 这一次我们先来演 ...

  9. javaweb二

    除了servlet规范,还有filter,listener.filter和servlet相似,但是在servlet之前执行,主要区别是有一个FilterChain接口可以执行拦截方法. import ...

  10. 手机端使用rem的适配

    <html> <body> <!-- http://www.w3cfuns.com/notes/29143/79dafb7c07f6865f435af641869d312 ...