递归二分查找 java_Java递归算法——二分查找

c15bb7637df786b2bcaa44dc3b70cad4.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]

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

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

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值