查找算法总结Java实现
之前对查找算法做的一些简单总结与实现:
查找算法时间复杂度:

1.二分查找的实现(待补充)
public class Test {
//循环实现二分查找
public static int binary(int[] array,int value){
int low=0;
int high=array.length-1;
while(low<=high){
int middle=(low+high)/2;
if(array[middle]==value){
return middle;
}
if(value<array[middle]){
high=middle-1;
}
if(value>array[middle]){
low=middle+1;
}
}
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int []array={1,3,12,45,56,67,68,78,79,123,234};
int m=Test.binary(array, 67);
System.out.println(m);
}
//递归实现二分查找
public static boolean recurseBinarySearch(int[] array,int n){
int start=0;
int end=array.length-1;
return bS(array,start,end,n);
}
public static boolean bS(int[] array,int start, int end,int n){
if(end<start)
return false;
int middle=(start+end)/2;
if(array[middle]>n)
return bS(array,start,middle-1,n);
else if(array[middle]<n)
return bS(array,middle+1,end,n);
else
return true;
}
}
2.hash查找算法(哈希函数、解决冲突)
public class HashSerash {
public int hashSearch(int[] hash,int length,int key){
int hashIndex=key%length;
while(hash[hashIndex]!=0&&hash[hashIndex]!=key){
hashIndex=(++hashIndex)%length;//如果不为0且有其他值,则去寻找下一个位置,直到为0或者哈希值等于key值--开放地址解决冲突
}
if(hash[hashIndex]==0)
return -1;
return hashIndex;
}
public void hashInsert(int[] hash,int length,int key){
int hashIndex=key%length;//取余法确定哈希函数
while(hash[hashIndex]!=0){
hashIndex=(++hashIndex)%length;//如果不为0则去寻找下一个位置,直到为0则存储--开放地址解决冲突
}
hash[hashIndex]=key;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array1=new int[]{2,43,321,6,119,5,34,1};
int length=array1.length+3;
int[] hash=new int[length];
for (int i = 0; i < array1.length; i++) {
System.out.print(array1[i]+",");
}
System.out.println("\n");
HashSerash hs=new HashSerash();
for (int i = 0; i < array1.length; i++) {
hs.hashInsert(hash, length, array1[i]);
}
int m=hs.hashSearch(hash, length, 6);
if(m==-1){
System.out.println("不在");
}else{
System.out.println("索引位置:"+m);
}
}
}
3. 二叉查找树(二叉排序树)
//构建二叉排序树
public static BinaryTree binarySearchTree(BinaryTree head,int k){
if(head==null){
head= new BinaryTree(k);
return head;
}else{
if(k<=head.value){
head.left=binarySearchTree(head.left,k);
}else{
head.right=binarySearchTree(head.right,k);
}
}
return head;
}
//查找二叉排序树中的节点
public static BinaryTree findTree(BinaryTree head,int k){
if(head==null)
return null;
else{
if(k==head.value)
return head;
else if(k<head.value){
return findTree(head.left,k);
}
else if(k>head.value){
return findTree(head.right,k);
}
}
return null;
}
//中序遍历,二叉树中序遍历为顺序
public static void midPrint(BinaryTree head){
if(head!=null){
midPrint(head.left);
System.out.println(head.value);
midPrint(head.right);
}
查找算法总结Java实现的更多相关文章
- 常用查找算法(Java)
常用查找算法(Java) 2018-01-22 1 顺序查找 就是一个一个依次查找 2 二分查找 二分查找(Binary Search)也叫作折半查找. 二分查找有两个要求, 一个是数列有序, 另一个 ...
- 查找算法(Java实现)
1.二分查找算法 package other; public class BinarySearch { /* * 循环实现二分查找算法arr 已排好序的数组x 需要查找的数-1 无法查到数据 */ p ...
- 二分查找算法,java实现
二分查找算法是在有序数组中用到的较为频繁的一种算法. 在未接触二分查找算法时,最通用的一种做法是,对数组进行遍历,跟每个元素进行比较,其时间复杂度为O(n),但二分查找算法则更优,因为其查找时间复杂度 ...
- 二分查找算法(JAVA)
1.二分查找又称折半查找,它是一种效率较高的查找方法. 2.二分查找要求:(1)必须采用顺序存储结构 (2).必须按关键字大小有序排列 3.原理:将数组分为三部分,依次是中值(所谓的中值就是数组中间位 ...
- 常见查找算法(Java代码实现)
一,顺序查找 查找算法中顺序查找算是最简单的了,无论是有序的还是无序的都可以,只需要一个个对比即可,但其实效率很低.我们来看下代码 public static int search(int[] a, ...
- 三大查找算法(Java实现)
三大查找算法 1.二分查找(Binary Search) public class BinarySearch { public static void main(String[] args) { in ...
- 二分查找算法的java实现
1.算法思想: 二分查找又称折半查找,它是一种效率较高的查找方法. 时间复杂度:O(nlogn) 二分算法步骤描述: ① 首先在有序序列中确定整个查找区间的中间位置 mid = ( low + ...
- Java实现的二分查找算法
二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...
- Java中常用的查找算法——顺序查找和二分查找
Java中常用的查找算法——顺序查找和二分查找 神话丿小王子的博客 一.顺序查找: a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数据最后一位 ...
随机推荐
- Nginx一直报504超时,配置相关参数好了
相关参数:large_client_header_buffers 4 16k;client_max_body_size 30m;client_body_buffer_size 128k;proxy_c ...
- android开发过程中项目中遇到的坑----布点问题
我们在红点push 的到达和点击的地方,都加了布点.后来功能上了线,发现,每天的点击都比到达高! 这肯定不科学. 赶紧查问题,打开程序,发红点,关闭程序,布点上传.没问题.数据部门可以收到红点啊! 从 ...
- LARK BOARD开发板试用第一篇-上电测试学习
1. 先看下板子外观,做工很不错 2. 主芯片的型号是,SoC 为 Cyclone V SX 系列的 5CSXFC6D6F31,不仅在芯片中包含传统的 FPGA 架构,还集成了基于 ARM Corte ...
- 对mysqlbinlog日志进行操作的总结包括 启用,过期自动删除
操作命令: show binlog events in 'binlog.000016' limit 10; reset master 删除所有的二进制日志 flush logs 产生一个新的binl ...
- 【个人训练】(ZOJ3983)Crusaders Quest
题意分析 和祖玛类似的那种玩法.不过是限定了九个字符,问最好情况下有几次三连碰. 暴力穷举即可.具体的做法是,先把所有"成块"的字符记录下来,然后一个一个删,再继续这样子递归做下去 ...
- CSP201509-1:数组分段
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的“计算机职业资格认证”考试,针对计算机软件开发. ...
- 企业级Tomcat部署配置
1.1 Tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人 ...
- React错误总结解决方案(二)
1.React native: Cannot add a child that doesn't have a YogaNode or parent node 该错误一般是因为render方法中注释语句 ...
- MySQL 5.6查看数据库的大小
1. use information_schema; 2. select concat(round(sum(data_length/1024/1024),2),'MB') as data from t ...
- Daily Scrum02 12.05
deadline果然是第一生产力...这学期一下子4~5个大的Project.然后截止日期都在近期.所有的组员都很辛苦!大家加油~ 这个scrum是当天过后一天补上的.因为当前负责的同学正在忙于编译大 ...