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));
} }

Java递归算法——二分查找的更多相关文章

  1. Java实现二分查找算法

    Java程序员总该玩点基本的算法. 1.前提:二分查找的前提是需要查找的数组必须是已排序的,我们这里的实现默认为升序 2.原理:将数组分为三部分,依次是中值(所谓的中值就是数组中间位置的那个值)前,中 ...

  2. 【15】-java实现二分查找

    二分查找在面试中经常被遇到,这个方法十分优雅 介绍 二分查找可以解决(预排序数组的查找)问题:只要数组中包含T(即要查找的值),那么通过不断缩小包含T的范围,最终就可以找到它.一开始,范围覆盖整个数组 ...

  3. 手把手教你用java实现二分查找树及其相关操作

    二分查找树(Binary Search Tree)的基本操作有搜索.求最大值.求最小值.求前继.求后继.插入及删除. 对二分查找树的进行基本操作所花费的时间与树的高度成比例.例如有n个节点的完全二叉树 ...

  4. java实现二分查找

    /** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...

  5. java 实现二分查找法

    /** * 二分查找又称折半查找,它是一种效率较高的查找方法. [二分查找要求]:1.必须采用顺序存储结构 2.必须按关键字大小有序排列. * @author Administrator * */ p ...

  6. Java算法 -- 二分查找

    折半查找,要求待查找的序列有序.每次取中间位置的值与待查关键字比较,如果中间位置的值比待查关键字大,则在前半部分循环这个查找的过程,如果中间位置的值比待查关键字小,则在后半部分循环这个查找的过程.直到 ...

  7. Java之二分查找算法

    算法说明:取中间位置的值与待查字比较.如果比待查字更大,则去列表的前半部分查找,如果比待查字小,则去列表的后半部分查找,直到找到这个待查字,或者返回没有找到这个待查字.其中给定的列表是从大到小排列的有 ...

  8. Java的二分查找

    今天学习了二分查找,虽然代码简单,但还是要有必要,记录一下今天的学习的. public class TestBrinarySeach { public static void main(String[ ...

  9. java 冒泡排序 二分查找 选择排序 插入排序

    下面这个程序是先定义一个整型数组,然后将其中的元素反序赋值,再用冒泡排序进行排序以后用二分查找来查找其中是否有某个数,返回值为-1时表示这个数可能小于这个数组的最小值或大小这个数组的最大值,-2表示这 ...

随机推荐

  1. SimpleDateFormate的使用

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...

  2. [转] Java序列化与反序列化

    原文地址:http://blog.csdn.net/wangloveall/article/details/7992448 Java序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java ...

  3. 【BZOJ 3242】【UOJ #126】【CodeVS 3047】【NOI 2013】快餐店

    http://www.lydsy.com/JudgeOnline/problem.php?id=3242 http://uoj.ac/problem/126 http://codevs.cn/prob ...

  4. vim——打开多个文件、同时显示多个文件、在文件之间切换

    打开多个文件: 1.vim还没有启动的时候: 在终端里输入  vim file1 file2 ... filen便可以打开所有想要打开的文件 2.vim已经启动 输入 :open file 可以再打开 ...

  5. 【Codeforces 707A】Brain's Photos 水题

    黑白灰都是#Black&White #include <cstdio> int n,m; int main() { scanf("%d%d",&n,&a ...

  6. How to fix the sources list

    How to fix the sources list Sometimes the apt-get may not work, it is often caused by the misspelled ...

  7. Linux基础1

    1.Linux文件系统类型 ext2 ext3(rhel5) ext4(rhel6) lvm raid swap gfs nfs vfat 2.linux 系统通过磁盘接口识别磁盘 IDE接口 hda ...

  8. .net读写config appsetting

    读 this.txtOutPutPath.Text = ConfigurationManager.AppSettings["OutPutPath"]; this.txtFilter ...

  9. oracle的resetlogs机制浅析(转)

    文章转自:http://blog.csdn.net/wyzxg/article/details/5869543 alter database open resetlogs 这个命令我想大家都很熟悉了, ...

  10. 遍历map集合

    for(var key in map){ if(data.hasOwnProperty(key)){ var value = map[key]; } }​