Java简单算法-二分查找

public class Main{
    public static void main(String[] args){
        int[] arr={10,20,30,40,50,60,70};
        int i =20;
        System.out.println(binarySearch(arr,i));
    }
    public static int binarySearch(int[] arr,int number){
        int min =0;
        int max=arr.length-1;
        while(true){
            if(min>max){
                return -1;
            }
            int mid=(min+max)/2;
            if(arr[mid]>number){
                max=mid-1;
            } else if (arr[mid]

你可能感兴趣的:(java,算法,数据结构)