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

 

posted @ 2017-02-26 17:08  JetHu  阅读(373)  评论(0)    收藏  举报