[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主要用于报文处理,通过绕过网络栈提高报 ...
随机推荐
- 结对编程—黄金点游戏WinForm单机版
本小游戏场景来自邹欣老师的<移山之道>一书: "阿超的课都是下午两点钟,这时班上不少的同学都昏昏欲睡,为了让大家兴奋起来,阿超让同学玩一个叫"黄金点"的游戏: ...
- 为PetaPoco添加实体模板
Brad为我们提供了T4模板,因为公司一直在使用CodeSmith,故为其写了一个CodeSmith的模板,代码如下: <%-- Name:EntityTemplates Author: Des ...
- Android自定义View
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View ...
- ambari之hbase数据迁移
一.hbase原理剖析 Base是一个构建在HDFS上的分布式列存储系统:HBase是基于Google BigTable模型开发的,典型的key/value系统:HBase是Apache Hadoop ...
- flex垂直居中
最近遇到一个令我绞尽脑汁的布局 T.T.T.T,分享下.重点--垂直居中. 布局说明:1. 场次为一场比赛 2. 比赛双方是交战的两个队伍 3. 一场比赛可以有多种玩法,所以场的每个玩法的布局的高度都 ...
- 我的android学习经历32
android系统架构 1.Linux内核层(LINUX KERNEL) 这是系统架构的最低层,这层为android设备的硬件提供了驱动 2.系统运行库层(LIBRARIES) 这一层为倒数第二层,利 ...
- 20.fastDFS集群java代码测试
1.工程结构 2.代码内容 FastdfsClientTest.java代码 package cn.itcast.fastdfs.cliennt; import java.io.File; i ...
- 配置org.springframework.scheduling.quartz.CronTriggerBean (转载)
在项目中又用到了定时器,对于定时器的应用总是模模糊糊的,今天结合网上找到的资料与自己在项目中写的简单地在此写一下,以备需要时查阅. 一个Quartz的CronTrigger表达式分为七项子表达式,其中 ...
- Spring 框架下Controller 返回结果在EasyUI显示
这几天弄了一下java下的在后台返回数据到jsp页面上的显示: 总结一下: 首先后台方面: @RequestMapping(value="/searchByUserName") @ ...
- Django1.9开发博客(14)- 集成Xadmin
xadmin是一个django的管理后台实现,使用了更加灵活的架构设计及Bootstrap UI框架, 目的是替换现有的admin,国人开发,有许多新的特性: 兼容 Django Admin 使用 B ...