[Algorithm Basics] Search
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的更多相关文章
- [Algorithm] A* Search Algorithm Basic
A* is a best-first search, meaning that it solves problems by searching amoung all possible paths to ...
- [Algorithm Basics] Sorting, LinkedList
Time complexity: Binary search O(log2 n): i=0. n elements: ------------------- i=1. n/2 ...
- [Math] Beating the binary search algorithm – interpolation search, galloping search
From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...
- [Algorithm] Beating the Binary Search algorithm – Interpolation Search, Galloping Search
From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...
- Algorithm | Binary Search
花了半天把二分查找的几种都写了一遍.验证了一下.二分查找的正确编写的关键就是,确保循环的初始.循环不变式能够保证一致. 可以先从循环里面确定循环不变式,然后再推导初始条件,最后根据循环不变式的内容推导 ...
- 【pat】algorithm常用函数整理
reference is_permutation Test whether range is permutation of another Parameters first1, last1 Input ...
- 本人AI知识体系导航 - AI menu
Relevant Readable Links Name Interesting topic Comment Edwin Chen 非参贝叶斯 徐亦达老板 Dirichlet Process 学习 ...
- [C7] Andrew Ng - Sequence Models
About this Course This course will teach you how to build models for natural language, audio, and ot ...
- BPF for storage:一种受外核启发的反式
BPF for storage:一种受外核启发的反式 译自:BPF for storage: an exokernel-inspired approach BPF主要用于报文处理,通过绕过网络栈提高报 ...
随机推荐
- Flowplayer-Setup
SOURCE URL: https://flowplayer.org/docs/setup.html 1. DOCTYPE At the top of your page declare the HT ...
- CSS3的chapter3
CSS的常用样式分为几大类: 字体样式(font,color, text-decoration ,text-shadow) 元素样式(width,height,margin,padding,opaci ...
- springMVC简单示例
1.新建web工程 2.引入springframework架包 3.配置文件 web.xml <?xml version="1.0" encoding="UTF-8 ...
- phonegap 3.3教程 地理信息api教程
一 准备工作 phonegap3.3的地理信息教程.从零开始,首先要新建一个项目从命令行启动 可以看到这是默认的生成的www目录,在这个目录里是最原始的html文件,编译的时候在根据这里的文件生成an ...
- python打怪之路【第二篇】:ImportError: No module named setuptools
在python安装第三方模块时出现如下错误: python错误:ImportError: No module named setuptools这句错误提示的表面意思是:没有setuptools的模块, ...
- LinkedList源码分析
LinkedList也和ArrayList一样实现了List接口,但是它执行插入和删除操作时比ArrayList更加高效,因为它是基于链表的.基于链表也决定了它在随机访问方面要比ArrayList逊色 ...
- 使用spark与ElasticSearch交互
使用 elasticsearch-hadoop 包,可在 github 中搜索到该项目 项目地址 example import org.elasticsearch.spark._ import org ...
- Ubuntu配置pyethapp
1. 安装系统依赖 apt-get install build-essential automake pkg-config libtool libffi-dev libgmp-dev 2. Clone ...
- UNIX-LINUX编程实践教程->第八章->实例代码注解->写一个简单的shell
一 分析 要实现一个shell,需包含3个步骤 1)读入指令 2)指令解析 3)执行指令 1 从键盘读入指令 从键盘读入指令的几个要点: 1)调用getc函数等待并获取用户键盘输入. 2)每一行命令的 ...
- html5的spellcheck属性(拼写、文法检查)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...