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. double函数和int函数

    可以看到,当tensor全是double型时,int函数会把所有元素取整,从1.5可以看出,不是四舍五入,而是取整.double函数又把整数型元素变成double型. th> a 0.0000 ...

  2. c++实现螺旋矩阵分析总结

    螺旋矩阵,是这么一个东西: 1   2   3 8   9   4 7   6   5 这是一个,n*n的矩阵,由外向里一次递增,一环一环,就好像一个螺旋一样.不难想象,如果n=5,那么应该是这样的: ...

  3. 设置MySQL自动增长从某个指定的数开始

    自增字段,一定要设置为primary key. 以指定从1000开始为例.1 创建表的时候就设置: CREATE TABLE `Test` ( `ID` int(11) NOT NULL AUTO_I ...

  4. 关于js执行顺序

    http://www.cnblogs.com/sanshi/archive/2011/02/28/1967367.html http://mtnt2008.iteye.com/blog/701981 ...

  5. 深入浅出设计模式——原型模式(Prototype Pattern)

    模式动机在面向对象系统中,使用原型模式来复制一个对象自身,从而克隆出多个与原型对象一模一样的对象.在软件系统中,有些对象的创建过程较为复杂,而且有时候需要频繁创建,原型模式通过给出一个原型对象来指明所 ...

  6. fifter常见的运用场景

    没配置过滤器 package servlet; import java.io.IOException; import javax.servlet.ServletException; import ja ...

  7. Knockout学习笔记之一

    1.  四大关键理念: A. DeclarativeBindings(声明式绑定) Easily associate DOM elements with model data using a conc ...

  8. 【CSS3】标签使用说明

    转换(transform):改变元素的形状.大小和位置. transform:rotate(20deg):顺时针旋转20° rotate()用来2D旋转改变角度.支持负数,表示逆时针. transfo ...

  9. ASP.NET MVC ActionResult的其它返回值

    一.ascx页面 场景:要返回代码片断,比如Ajax返回一个子页 我们先新建一个Action public ActionResult Ascx() { return PartialView(); } ...

  10. iOS开发多线程篇—GCD介绍

    iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...