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基础(三元运算+深浅拷贝+函数参数)

    三元运算 三元运算,又称三目运算,主要作用是减少代码量,是对简单的条件语句的缩写. 书写格式: result = 值1 if 条件 else 值2 即如果条件成立,则将值1赋给result变量,如果不 ...

  2. Robotlegs框架1.5简介

    该框架的1.5版本位于https://github.com/robotlegs/robotlegs-framework/tree/version1,现在已经出了重新架构的2.0版本,所以我决定先研究已 ...

  3. 使用git建立远程仓库,让别人git clone下来

    首先, 如果你的ssh没有安装的话,要安装ssh服务端.ubuntu是很简单 sudo apt-get install openssh-server 1,建立你的git 目录. ourunix@ubu ...

  4. nginx 配置文件备份

    1. /etc/nginx/sites-enabled/default 的原始文件 # You may add here your # server { # ... # } # statements ...

  5. Animating graphic objects in Windows Forms.

    原文: Animating graphic objects in Windows Forms. http://bobpowell.net/animation.aspx 文件下载备份:http://fi ...

  6. C# Reportviewer 固定表头

    RDLC报表固定每页都显示表头以XML方式打开rdlc文件查找到<TablixRowHierarchy> <TablixMembers> <TablixMember> ...

  7. 【leetcode❤python】 400. Nth Digit

    #-*- coding: UTF-8 -*- class Solution(object):    def findNthDigit(self, n):        ""&quo ...

  8. JS常规的验证代码 - 手机号,邮箱,字符串查找

    //在字符串中执行查找 function isDisgit(s){ var reg = /^[0-9]{1,20}$/; var result = reg.exec(s); //如果格式不正确,返回n ...

  9. D3.js 力导向图

    花了大半天看了一个八十几行的代码..心累 力导向图是之前就有画过很多次的东西,但是这次的代码看上去很陌生,然后发现是D3更新了4.0.... 先贴代码 var svg = d3.select(&quo ...

  10. Hibernate <查询缓存>

    查询缓存: 定义:查询缓存它是基于二级缓存的,可以保存普通属性查询的结果,查询对象实体时,他会保存id作为键,查询结果作为值,下个对象访问时,可以直接查到 查询缓存查询实体对象时,显著的特点是,会执行 ...