Java查找算法——二分查找
import java.lang.reflect.Array;
import java.nio.Buffer;
import java.util.Arrays;
import java.util.Random; //=================================================
// File Name : Binary_Search
//------------------------------------------------------------------------------
// Author : Common //类名:BinarySearch_Find
//属性:
//方法:
class BinarySearch_Find{
private int[] temp;
private int searchKey;
private int lowerBound = 0; //下界
private int upperBound ; //上界
private int curNum; public int[] getTemp() {
return temp;
} public void setTemp(int[] temp) {
this.temp = temp;
} public BinarySearch_Find(int[] temp) {//构造函数
this.temp = temp;
this.upperBound = temp.length-1;
} public int find(int searchKey){
this.searchKey = searchKey;
while(true){
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){
lowerBound = curNum+1;
}
else{
upperBound = curNum-1;
}
}
}
} } //类名:RandomArrays
//属性:
//方法:
class RandomArrays{ //生成随机数组,有Num个 public int[] getArrays(int Num){
int[] 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 OrderedArrays{ //生成有序数组,从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 : Binary_Search
public class Binary_Search { public static void main(String[] args) {
// TODO 自动生成的方法存根 // RandomArrays array_demo = new RandomArrays();
// BinarySearch_Find arrays = new BinarySearch_Find(array_demo.getArrays(100)); OrderedArrays array_demo = new OrderedArrays();
BinarySearch_Find arrays = new BinarySearch_Find(array_demo.getArrays(100));
System.out.println(Arrays.toString(arrays.getTemp()));
System.out.println(arrays.find(1000)); } }
Java查找算法——二分查找的更多相关文章
- C语言实现常用查找算法——二分查找
#include<stdio.h> void insert_sort(int a[],int n); int binary_search(int a[],int x,int n); voi ...
- 查找算法----二分查找与hash查找
二分查找 有序列表对于我们的实现搜索是很有用的.在顺序查找中,当我们与第一个元素进行比较时,如果第一个元素不是我们要查找的,则最多还有 n-1 个元素需要进行比较. 二分查找则是从中间元素开始,而不是 ...
- 各种查找算法的选用分析(顺序查找、二分查找、二叉平衡树、B树、红黑树、B+树)
目录 顺序查找 二分查找 二叉平衡树 B树 红黑树 B+树 参考文档 顺序查找 给你一组数,最自然的效率最低的查找算法是顺序查找--从头到尾挨个挨个遍历查找,它的时间复杂度为O(n). 二分查找 而另 ...
- Java实现的二分查找算法
二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...
- Java中常用的查找算法——顺序查找和二分查找
Java中常用的查找算法——顺序查找和二分查找 神话丿小王子的博客 一.顺序查找: a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数据最后一位 ...
- Java学习之二分查找算法
好久没写算法了.只记得递归方法..结果测试下爆栈了. 思路就是取范围的中间点,判断是不是要找的值,是就输出,不是就与范围的两个临界值比较大小,不断更新临界值直到找到为止,给定的集合一定是有序的. 自己 ...
- 经典算法二分查找循环实现Java版
二分查找 定义 二分查找(Binary Search)又称折半查找,它是一种效率较高的查找方法. 要求 (1)必须采用顺序存储结构 (2)必须按关键字大小有序排列 查找思路 首先将给定值K,与表中中间 ...
- Java顺序查找、二分查找
Java顺序查找.二分查找 查找算法中顺序查找算是最简单的了,无论是有序的还是无序的都可以,只需要一个个对比即可,但其实效率很低. 顺序查找 动图演示 详细代码 // 顺序查找 public st ...
- C语言查找算法之顺序查找、二分查找(折半查找)
C语言查找算法之顺序查找.二分查找(折半查找),最近考试要用到,网上也有很多例子,我觉得还是自己写的看得懂一些. 顺序查找 /*顺序查找 顺序查找是在一个已知无(或有序)序队列中找出与给定关键字相同的 ...
随机推荐
- struts2 spring3.2 hibernate4.1 框架搭建 整合
ssh是企业开发中常遇到的框架组合,现将框架的搭建过程记录下来,以便以后查看.我的搭建过程是,首先struts,然后spring,最后hibernate.struts2的最新版本为2.3.8,我下载的 ...
- [转]关于jquery中html()、text()、val()的区别
原文地址:http://www.cnblogs.com/xiaolifeidao/p/3715830.html .html()用为读取和修改元素的HTML标签 对应js中的innerHTML . ...
- iOS开发-- 利用AVPlayer播放远程音乐和视频
一.简单的播放音乐和视频,播放视频的工具栏需要自己写 二.利用老师封装的框架实现视频播放 链接:http://pan.baidu.com/s/1hrEKlus 密码:8e7g
- javascript 方法实例
输出对象的属性名称与值 : boj(o){ for(var p in o){ console.log(p + ":" + o[p] + "\n"); } } 构 ...
- zabbix的使用
1,zabbix运行流程 2功能特性 1数据收集 2灵活触发器 3高度可定制告警 4实时绘图功能 5web监控能力 6多种可视化展示 7历史数据存储 8配置容易 9API功能 10.......... ...
- linux下截取整个网页
前提需要安装 gimp图片处理软件 打开gimp 文件-创建-从网页 然后输入网页地址就可以截取整个网页了
- 使用fiddler查看https请求
首先点击菜单栏Tools>>>Fiddler Options>>>HTTPS 把Decrypt HTTPS Traffic 复选框勾选上 勾上之后,会弹窗提示你. ...
- 【CodeForces 697B】Barnicle
对科学计数法表示的数,输出其10进制的形式. c++来做,需要考虑这些细节: 当b==0,d==0时,只输出a. 当不需要补零的情况有两种: 一种是刚好是整数,只输出a(注意1.0e1的情况是输出1) ...
- block的使用
转载自:http://mobile.51cto.com/hot-403897.htm 一.概述 Block是C级别的语法和运行时特性.Block比较类似C函数,但是Block比之C函数,其灵活性体现在 ...
- Trinity min_kmer_cov
A high min_kmer value was used to reduce noise in the assembly and to identify only transcripts that ...