1, Binary Search

On sorted array!

public static int binarySearch(int e, int[] array, int low, int high) {
  while(low <= high) {
    int mid = (low+high)/2;
    if(e == array[mid]) return mid;
    else if(e < array[mid]) high = mid -1 ;
    else low = mid + 1;
  }
  return -1;
}

On Rotated sorted array:

while(low <= high) {
  int mid = (low+high)/2;
  if(array[mid] == e) return mid;
  if(array[mid] < array[high]) { //the 2nd half is sorted
    if(e > array[mid] && e< array[high])
      low = mid+1;
    else
      high = mid-1;
  }
  if(array[low] < array[mid]) { //the 1st half is sorted
    if(e < array[mid] && e> array[low])
      high = mid-1;
    else
      low = mid+1;
  }
}

Rotate array by position x:

Reverse the whole array, then Reverse 0~x-1, then Reverse x~size-1.

2, Fibonacci

f(n)= f(n-1)+f(n-2)

对于实现函数calculateFibo(int index),可以用while loop来逐一计算每个index上的fibo直到到达指定的index;也可以recursive地调用f(n-1) f(n-2)。

对于recursive的方法,类似于一个深度为n的tree,每向下扩展一次结点都会有两个分支,所以O(n)=2*2*2... = 2^n复杂度。

[Algorithm Basics] Search的更多相关文章

  1. [Algorithm] A* Search Algorithm Basic

    A* is a best-first search, meaning that it solves problems by searching amoung all possible paths to ...

  2. [Algorithm Basics] Sorting, LinkedList

    Time complexity: Binary search O(log2 n): i=0.   n elements:         ------------------- i=1.   n/2 ...

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

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

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

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

  5. Algorithm | Binary Search

    花了半天把二分查找的几种都写了一遍.验证了一下.二分查找的正确编写的关键就是,确保循环的初始.循环不变式能够保证一致. 可以先从循环里面确定循环不变式,然后再推导初始条件,最后根据循环不变式的内容推导 ...

  6. 【pat】algorithm常用函数整理

    reference is_permutation Test whether range is permutation of another Parameters first1, last1 Input ...

  7. 本人AI知识体系导航 - AI menu

    Relevant Readable Links Name Interesting topic Comment Edwin Chen 非参贝叶斯   徐亦达老板 Dirichlet Process 学习 ...

  8. [C7] Andrew Ng - Sequence Models

    About this Course This course will teach you how to build models for natural language, audio, and ot ...

  9. BPF for storage:一种受外核启发的反式

    BPF for storage:一种受外核启发的反式 译自:BPF for storage: an exokernel-inspired approach BPF主要用于报文处理,通过绕过网络栈提高报 ...

随机推荐

  1. python简单实现用户表单登录

    实现简单的用户表单验证登录 user="desperado" pwd=" s=0 for i in range(10): if s < 3: username = ...

  2. [问题2015S05] 复旦高等代数 II(14级)每周一题(第六教学周)

    [问题2015S05]  设 \(A\) 是 \(n\) 阶复方阵, 证明: \(A\) 可对角化的充分必要条件是 \(A\) 相似于某个如下的循环矩阵: \[C=\begin{pmatrix} a_ ...

  3. Excel公式错误提示啥意思?

    1.#####!返回的结果超出了单元格的宽度:或者单元格的日期时间公式产生了一个负值. 2.#VALUE!使用了错误的参数或运算对象类型. 3.#DIV/O!当公式被零除时产生此错误. 4.#NAME ...

  4. nRF52系列来袭,Nordic的低功耗蓝牙方案大有可为

      坐落在北欧的挪威不像他的邻居芬兰那样,可以先后依靠NOKIA和愤怒的小鸟在世界科技界享有盛名.在一般人看来,挪威除了一个逐渐式微的Opera浏览器以外,并没有更多拿得出手的科技企业.而事实证明这只 ...

  5. 初学画布canvas的chapter1

    ——这篇读后感是我阅读<写给Web开发人员看的HTML5教程>一书中的第5章画布后的小小看法,由于编程实力有限,很多效果病没有一一去实现,所以只是停留在纸上谈兵的阶段. 画布(canvas ...

  6. C# Global Application_Error不执行

    今天在开发过程中遇到一个很奇特的问题,就是 Global 文件中的Application_Error 方法不执行的问题,很是苦恼,查了有关这方面的问题,感觉网友们回答的都有点乱,有些人说 在编译时不需 ...

  7. (转) vector的reserve和resize

    文章转自  http://www.cnblogs.com/qlee/archive/2011/05/16/2048026.html vector 的reserve增加了vector的capacity, ...

  8. createjs mask 填坑过程

    createjs 的mask必须使用 shape  (不算坑) 作为遮罩的shape不能被 addChild  (上一次 自己居然躲过了,这次被巨坑) var S=this; var shape = ...

  9. Navicat for mysql 破解

    想用navicat for mysql 连接mysql,发现只能试用30天,感觉挺不爽的,购买的话发现价格一千多,好贵的软件. 所以想要破解一下,网上试了一些方法不行,最后找到了一种方法可以的 破解工 ...

  10. struts入门初步(一)

      struts2.0与struts1.0运用了不同的框架,有一定的不兼容性. struts2.0借鉴了webwork的框架思想. Struts2的基本步骤: 1.拷贝struts的jar到项目中(导 ...