Java递归算法——二分查找
2016-04-07 09:39
981 查看
![](https://images2015.cnblogs.com/blog/517519/201604/517519-20160407093817937-2033053522.png)
import java.lang.reflect.Array; import java.nio.Buffer; import java.util.Arrays; import java.util.Random; //================================================= // File Name : RecFind //------------------------------------------------------------------------------ // Author : Common //类名:BinarySearch_Find //属性: //方法: class Rec_Find{ private int[] temp; private int searchKey; //private int lowerBound = 0; //下界 //private int upperBound ; //上界 private int nElement; public int[] getTemp() { return temp; } public void setTemp(int[] temp) { this.temp = temp; } public Rec_Find(int[] temp) {//构造函数 this.temp = temp; //this.upperBound = temp.length-1; } public int find(int searchKey,int lowerBound,int upperBound){ int curNum; this.searchKey = searchKey; curNum = (lowerBound+upperBound)/2; if(temp[curNum]==this.searchKey){ return curNum; //find } else if(lowerBound>upperBound){ return -1; //没有find } else{ if(temp[curNum]<this.searchKey){ return find(searchKey,curNum+1,upperBound); } else{ return find(searchKey,lowerBound,curNum-1); } } } } //类名:RandomArrays //属性: //方法: class RandomArray{ //生成随机数组,有Num个 private int[] Arrays; public int[] getArrays(int Num){ // int[] Arrays = new int[Num]; Arrays = new int[Num]; Random r = new Random(); for(int i=0;i<Num;i++){ Arrays[i] = r.nextInt(1000); // System.out.print(Arrays[i]+"、"); } return Arrays; } } //类名:OrderedArrays //属性: //方法: class OrderedArray{ //生成有序数组,从0开始到Num public int[] getArrays(int Num){ int[] Arrays = new int[Num]; for(int i=0;i<Num;i++){ Arrays[i] = i; // System.out.print(Arrays[i]+"、"); } return Arrays; } } //主类 //Function : RecFind public class RecFind { public static void main(String[] args) { // TODO 自动生成的方法存根 // RandomArrays array_demo = new RandomArrays(); // BinarySearch_Find arrays = new BinarySearch_Find(array_demo.getArrays(100)); OrderedArray array_demo = new OrderedArray(); Rec_Find arrays = new Rec_Find(array_demo.getArrays(100)); System.out.println(Arrays.toString(arrays.getTemp())); System.out.println(arrays.find(55,0,100)); } }
相关文章推荐
- springMVC配置html和jsp视图
- spring注解---@Resource和@Autowired
- 在eclipse部署静态web项目
- 在eclipse部署静态web项目
- MyEclipse中文乱码
- SpringMVC经典系列-14自己定义SpringMVC的拦截器---【LinusZhu】
- 使用spring的@Async异步执行
- struts2中自定义拦截器
- JAVA数据源连接方式汇总
- 69道java Spring面试题和答案
- Java进阶(二十六)公司项目开发知识点回顾
- Java进阶(二十六)公司项目开发知识点回顾
- Java RMI原理
- Spring基于注解TestContext 测试框架使用详解
- Eclipse反编译插件jad安装
- JVM内幕:Java虚拟机详解
- Struts2框架搭建步骤
- Java后端书架
- java验证码 工具类
- 《深入理解Java虚拟机》笔记