算法--二分法查找

    public static int BinarySearch(int[] arr, int el)
    {
        int low = 0;
        int high = arr.length - 1;
        int mid = 0;
        while(low < high)
        {
            mid = (high + low) / 2;
            if(arr[mid] > el)
            {
                high = mid - 1;
            }
            else if(arr[mid] < el)
            {
                low = low + 1;
            }
            else
            {
                return mid;
            }
        }
        return -1;
    }

 

你可能感兴趣的:(二分法)