之前对查找算法做的一些简单总结与实现:

查找算法时间复杂度:

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实现的更多相关文章

  1. 常用查找算法(Java)

    常用查找算法(Java) 2018-01-22 1 顺序查找 就是一个一个依次查找 2 二分查找 二分查找(Binary Search)也叫作折半查找. 二分查找有两个要求, 一个是数列有序, 另一个 ...

  2. 查找算法(Java实现)

    1.二分查找算法 package other; public class BinarySearch { /* * 循环实现二分查找算法arr 已排好序的数组x 需要查找的数-1 无法查到数据 */ p ...

  3. 二分查找算法,java实现

    二分查找算法是在有序数组中用到的较为频繁的一种算法. 在未接触二分查找算法时,最通用的一种做法是,对数组进行遍历,跟每个元素进行比较,其时间复杂度为O(n),但二分查找算法则更优,因为其查找时间复杂度 ...

  4. 二分查找算法(JAVA)

    1.二分查找又称折半查找,它是一种效率较高的查找方法. 2.二分查找要求:(1)必须采用顺序存储结构 (2).必须按关键字大小有序排列 3.原理:将数组分为三部分,依次是中值(所谓的中值就是数组中间位 ...

  5. 常见查找算法(Java代码实现)

    一,顺序查找 查找算法中顺序查找算是最简单的了,无论是有序的还是无序的都可以,只需要一个个对比即可,但其实效率很低.我们来看下代码 public static int search(int[] a, ...

  6. 三大查找算法(Java实现)

    三大查找算法 1.二分查找(Binary Search) public class BinarySearch { public static void main(String[] args) { in ...

  7. 二分查找算法的java实现

    1.算法思想: 二分查找又称折半查找,它是一种效率较高的查找方法.    时间复杂度:O(nlogn) 二分算法步骤描述: ① 首先在有序序列中确定整个查找区间的中间位置 mid = ( low + ...

  8. Java实现的二分查找算法

    二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...

  9. Java中常用的查找算法——顺序查找和二分查找

    Java中常用的查找算法——顺序查找和二分查找 神话丿小王子的博客 一.顺序查找: a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数据最后一位 ...

随机推荐

  1. 【python3.X】python学习中排雷过程^_^

    问题一:python读取文件时报错:“UnicodeDecodeError: 'gbk' codec can't decode byte 0x8d in position 52: illegal mu ...

  2. 第5模块闯关Bootstrap

    “行(row)”必须包含在 .container (固定宽度)或 .container-fluid (100% 宽度)中,以便为其赋予合适的排列(aligment)和内补(padding). 通过“行 ...

  3. 【好帖】 Mark

    1. 管理篇 2. 程序员选择公司的8个标准 3. 实用工具 4. 离职跳槽 5. DBA 6. 做一个网站多少钱? 7. 十大算法 8. 寻求用户评价App的正确方法 9. 工程师忽略的隐形成本 1 ...

  4. 构建Http服务器

    可以通过多种途径来构建服务器用以响应客户端请求(~不提供实现源码,网上有相应资源~) (1)使用ServerSocket构建服务器 (2)使用Servlet构建服务器 (3)使用HttpServer构 ...

  5. Qt Charts_Audio实践

    这里完全是照搬帮助文档中的代码生成的程序 上预览图 工程文件代码 #------------------------------------------------- # # Project crea ...

  6. 5.爬虫 requests库讲解 高级用法

    0.文件上传 import requests files = {'file': open('favicon.ico', 'rb')} response = requests.post("ht ...

  7. 贝叶斯网(1)尝试用Netica搭建简单的贝叶斯网并使用贝叶斯公式解释各个bar的结果

    近来对贝叶斯网十分感兴趣,按照博客<读懂概率图模型:你需要从基本概念和参数估计开始>给出的第一个例子,试着搭建了一个student网. (1)点击绿F,对条件概率表予以输入(包括两个祖先节 ...

  8. chrome提示Adobe Flash Player过期解决

    安装插件:install_flash_player_ppapi.exe

  9. php自学笔记2

    php运行原理: 如果请求服务器上的资源是html网页,服务器直接将网页响应给客户端浏览器: 如果请求服务器上的资源是php,服务器先解释执行php,解释为标准的html代码响应给客户端浏览器.php ...

  10. 【iOS开发】iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 150)]; label.backgroundColor ...