7月19日学习日志

1.今天学习了查找算法,有顺序查找和二分查找,二分查找是一个非常重要的算法。

下面是二分查找的代码:

public class BinarySearchDemo{
    public static void main(String[] args){
        int[] a={2,3,4,5,9,7,8};
        int value=5;
        int count=-1;
        int low=0,high=a.length-1;
        while(low<high){
            int mid=(low+high)/2;
            if(a[mid]==value){
                count=mid;
                break;
            }
            else if(a[mid]>value){
                high=mid-1;
            }
            else {
                low=mid+1
            }
            if(count!=-1) 
                System.out.println("下标在:"+count+"位置");
            else
                System.out.println("没有找到");
    }
}

2.没有遇到问题。

3.明天计划学习工具类中算法的实现。

posted @ 2020-07-19 20:54  张笑天  阅读(109)  评论(0编辑  收藏  举报