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

查找算法时间复杂度:

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. 『JavaScript』核心

    弱类型语言 JavaScript是一种弱类型的语言.变量可以根据所赋的值改变类型.原始类型之间也可以进行类型转换.其弱类型的物质为其带来了极大的灵活性. 注意:原始类型使用值传递,复合类型使用引用传递 ...

  2. Python中该使用%还是format来格式化字符串?

    %还是format 1.皇城PK Python中格式化字符串目前有两种阵营:%和format,我们应该选择哪种呢? 自从Python2.6引入了format这个格式化字符串的方法之后,我认为%还是fo ...

  3. 《python核心编程第二版》第8章习题

    8–1. 条件语句. 请看下边的代码 # statement Aif x > 0:# statement Bpasselif x < 0:# statement Cpasselse:# s ...

  4. Linux-获得命令帮助man

    date:显示当前系统时间,修改时间 clock,hwclock:显示硬件时间 cal:calendar,查看日历 计时器靠晶体振荡器来完成计时 Linux: 实时时钟,rtc,real time c ...

  5. 从底层带你理解Python中的一些内部机制

    下面博文将带你创建一个字节码级别的追踪API以追踪Python的一些内部机制,比如类似YIELDVALUE.YIELDFROM操作码的实现,推式构造列表(List Comprehensions).生成 ...

  6. PhpStorm 配置IDE

    IDE => Xdebug => Apache(XAMPP) => Firefox + easist Xdebug 1>XAMPP停止apache服务;2>在安装目录下找 ...

  7. ORACLE和SQL语法区别归纳

    数据类型比较类型名称 Oracle   SQLServer   比较字符数据类型  CHAR  CHAR  都是固定长度字符资料但oracle里面最大度为2kb,SQLServer里面最大长度为8kb ...

  8. hibernate对单表的增删改查

    ORM: 对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping) 实现对单表的增删改查 向区域表中增加数据: 第一步: 新建一个Da ...

  9. tomcat 路径"/"表示根目录

  10. JQuery事件对象的属性和方法

    这是今天的总结,以后学习自己可以当参考书来读读.Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态.事件通常与函数结合使用,函数不会在事件发生前被执行 ...