• 查找算法总结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(" ");

          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开发web的几种开发模式
    Tomcat7.0安装配置
    Apache与Tomcat 区别联系
    编写 Window 服务程序
    系列文章--SharePoint 开发教程
    Google Chrome浏览器调试功能介绍
    Chrome的JS调试工具
    Google Chrome 调试JS简单教程[更新]
    ASP.NET常用标准配置web.config
    WINDOWS下kill进程的命令
  • 原文地址:https://www.cnblogs.com/jetHu/p/6445117.html
Copyright © 2020-2023  润新知