二分查找和插值查找

查找

顺序查找

省略

二分查找

binary Search

  • 有序数组
/**
 * @author yyq
 * @create 2021-10-22 14:37
 * 二分查找
 * 前提是有序数组
 */
public class BinaryFind {

    public static int binaryFind(int[] a,int val){
        int low=0;
        int high=a.length-1;
        int mid=(low+high)/2;
        while(low<high){
            if(a[mid]==val){
                return mid;
            }
            if(a[mid]<val){
                low=mid+1;
                mid=(low+high)/2;
                continue;
            }
            if(a[mid]>val){
                high=mid-1;
                mid=(low+high)/2;
                continue;
            }
        }
      //找不到
        return -1;
    }

    public static void main(String[] args) {
        int[] a={1,8, 10, 89, 1000, 1234};
        int i = binaryFind(a, 0);
        if(i==-1) System.out.println("没找到");
        else System.out.println("找到了"+i);
    }
}
思考题

课后思考题: {1,8, 10, 89, 1000, 1000,1234} 当一个有序数组中,有多个相同的数值时,如何将所有的数值都查找到,比如这里的 1000.

/**
 * @author yyq
 * @create 2021-10-22 14:37
 * 二分查找
 * 前提是有序数组
 * 找到了数据之后需要向左或者向右探测是否有相同的。
 */
public class BinaryFind_xt {

    public static int[] binaryFind(int[] a,int val){
        int low=0;
        int high=a.length-1;
        int mid=(low+high)/2;
        while(low<high){
            if(a[mid]==val){
                int[] b=new int[10];
                int i=0;
                b[i]=mid;
                i++;

                int temp=mid+1;
                //向右探测
                while (true)
                {
                    //需要分别往左右两个方向探测
                    if(a[temp]!=val) break;
                    b[i]=temp;
                    temp++;
                    i++;
                }
                temp=mid-1;
                //向左探测
                while (true){
                    //需要分别往左右两个方向探测
                    if(a[temp]!=val) break;
                    b[i]=temp;
                    temp--;
                    i++;
                }
                return b;
            }
            if(a[mid]<val){
                low=mid+1;
                mid=(low+high)/2;
                continue;
            }
            if(a[mid]>val){
                high=mid-1;
                mid=(low+high)/2;
                continue;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        int[] a={1,8, 10, 89, 1000,1000,1000, 1234};
        int[] ints = binaryFind(a, 1000);
        if(ints==null) System.out.println("没找到");
        else {
            for (int i = 0; i < ints.length; i++) {

                System.out.println("找到了" + ints[i]);
            }
        }
    }
}

插值查找

*interpolation search

插值查找算法类似于二分查找,不同的是插值查找每次从自适应mid处开始查找。

有点像分成段成比例

package Search;

/**
 * @author yyq
 * @create 2021-10-22 14:37
 * 插值查找
 * 与二分查找最大的不同是 自适应确定mid
 * 二分查找 mid=(low+high)/2
 * 插值查找 mid=low+(val-a[low])*(a[high]-a[low])/(high-low)
 * 前提是有序数组
 */
public class InterpolationSearch {

    public static int binaryFind(int[] a,int val){
        int low=0;
        int high=a.length-1;
        int mid=low+(val-a[low])/(a[high]-a[low])*(high-low);
        while(low<high){
            if(mid-1<0||mid+1>a.length) break;
            if(a[mid]==val){
                return mid;
            }
            if(a[mid]<val){
                low=mid+1;
                mid=low+(val-a[low])*(a[high]-a[low])/(high-low);
                continue;
            }
            if(a[mid]>val){
                high=mid-1;
                mid=low+(val-a[low])*(a[high]-a[low])/(high-low);
                continue;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] a={1,8, 10, 89, 1000, 1234};
        int i = binaryFind(a, 1234);
        if(i==-1) System.out.println("没找到");
        else System.out.println("找到了"+i);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值