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. Cookie与Session的区别

    cookie机制 Cookies是服务器在本地机器上存储的小段文本并随每一个请求发送至同一个服务器.IETF RFC 2965 HTTP State Management Mechanism 是通用c ...

  2. POJ 1113:Wall

    原文链接:https://www.dreamwings.cn/poj1113/2832.html Wall Time Limit: 1000MS   Memory Limit: 10000K Tota ...

  3. 搭建自己本地yum源

    1.挂载系统光盘(注:medi下的cdrom是我自己创建的,可以挂载在任意目录) [root@liutao ~]# mount /dev/cdrom /media/cdrom/ 2.修改yum配置文件 ...

  4. Linux常用调优配置

    cenos 6.5 文件句柄和网络端口 修改系统所有进程可用句柄数,vi /etc/sysctl.conf fs.file-max=655360net.ipv4.ip_local_port_range ...

  5. assigning to 'id<UIGestureRecognizerDelegate> _Nullable' from incompatible

    tip:参考 http://stackoverflow.com/questions/9861538/assigning-to-iddelegate-from-incompatible-type-vie ...

  6. hdu2243考研路茫茫——单词情结(ac+二分矩阵)

    链接 跟2778差不多,解决了那道题这道也不成问题如果做过基本的矩阵问题. 数比较大,需要用unsigned longlong 就不需要mod了 溢出就相当于取余 #include <iostr ...

  7. 如何低成本的打造HTC Vive虚拟演播室直播MR视频?

    http://m.toutiao.com/i6298923859378700802/?tt_from=weixin&utm_campaign=client_share&from=gro ...

  8. null 和 undefined 的区别

    null表示"没有对象",即该处不应该有值. (1) 作为函数的参数,表示该函数的参数不是对象. (2) 作为对象原型链的终点. undefiend 就是一个缺少值,此处应该有的值 ...

  9. python 安装模块步骤

    1.下载 pyocr-0.4.1.tar.gz   tar.gz文件  解压  放到 c:/python27 文件夹下面 C:\Python27\pyocr-0.4.1  直接 cmd 命令 进入   ...

  10. MySQL drop、delete和truncate的区别

    注意:这里说的delete是指不带where子句的delete语句 相同点 truncate和不带where子句的delete, 以及drop都会删除表内的数据 不同点: 1. truncate和 d ...