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

查找算法时间复杂度:

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. 做模态弹框的时候,防止背景滚动方法 移动端 html5

    $(window.document).bind("touchmove", function() { return false; });

  2. Spring框架中ModelAndView、Model、ModelMap的区别

    转自:http://blog.csdn.net/liujiakunit/article/details/51733211 1. Model Model 是一个接口, 其实现类为ExtendedMode ...

  3. CentOS环境配置Hadoop(一)

    配置Linux开发环境(hadoop-2.6.4) 一.准备工具 VMware-workstation-10.0.1注册机 CentOS-6.5-x86_64-bin-DVD1 jdk-7u79-li ...

  4. [PocketFlow]解决TensorFLow在COCO数据集上训练挂起无输出的bug

    1. 引言 因项目要求,需要在PocketFlow中添加一套PeleeNet-SSD和COCO的API,具体为在datasets文件夹下添加coco_dataset.py, 在nets下添加pelee ...

  5. 图解Transformer

    图解Transformer 前言 Attention这种机制最开始应用于机器翻译的任务中,并且取得了巨大的成就,因而在最近的深度学习模型中受到了大量的关注.在在这个基础上,我们提出一种完全基于Atte ...

  6. C++学习---- virtual的三种用法

    virtual用法一:多态 #include<iostream> using namespace std; class A{ public: virtual void display(){ ...

  7. 通过 systemctl 设置自定义 Service

    如果要在Linux 上设置一个开机自启,出现问题自动重启,并且有良好日志的程序,比较流行的方法有 supervisord.systemd,除此之外,还有 upstart.runit 等类似的工具. 但 ...

  8. Win7下搭建Zigbee开发环境

    操作系统:64位Win7 芯片类型:Texas Instruments的CC2530 软件平台:IAR v8.10 Zigbee协议栈:ZStack-CC2530-2.5.1a CP2102 USB ...

  9. MySQL 5.6查看数据库的大小

    1. use information_schema; 2. select concat(round(sum(data_length/1024/1024),2),'MB') as data from t ...

  10. Java 端口扫描器 TCP的实现方法

    想必很多朋友都实现过一个简易的聊天室这个功能,其中涉及到Socket套接字这个类,我们通过一个特定的IP以及特定的端口创建一个服务端的套接字(ServerSocket),以此我们聊天个体的套接字(So ...