java 二分法/插值查找算法

package com.example.lib;

public class SearchUtils {
    public static void main(String[] args) {
        int [] list=new int[]{4,2,3,1,6,5};
        int value= binarysearch(list,3);
        System.out.print(value);
    }

    //二分法查找
    public static int binarysearch(int[] list,int search){
         fastSort(list,0,list.length-1);//o(nlogn)
         return search2(list,search,0,list.length-1);
    }

    public static  int search(int[] list,int search,int low,int height){
        if(low>height){
            return -1;
        }

        int mid=low+(height-low)/2;
        if(searchlist[mid]){
            return  search(list,search,mid+1,height);
        }else{
            return  mid;
        }

    }

    //插值查找
    public static  int search2(int[] list,int search,int low,int height){
        if(low>height){
            return -1;
        }

        int mid=low+(search-low)*((search-list[low])/(list[height]-list[low]));
        if(searchlist[mid]){
            return  search2(list,search,mid+1,height);
        }else{
            return  mid;
        }

    }

   //快排
    public static void fastSort(int[] arrays,int low,int height){
        //一定义base 比base大放右边,小大放左边
        if(low>=height){
            return;
        }
        int l=low;
        int r=height;
        int base=low;

        while (larrays[base]){
                r--;
            }

            if(l

 

你可能感兴趣的:(java算法学习)