greyhh

记录学习中的点点滴滴

导航

二分查找算法

Posted on 2015-08-07 00:58  greyhh  阅读(137)  评论(0编辑  收藏  举报
public static int BinarySearch(int[] arr, int low, int high, int key)

{

    int mid = (low + high) / 2;

    if (low > high)

        return -1;

    else

    {

        if (arr[mid] == key)

            return mid;

        else if (arr[mid] > key)

            return BinarySearch(arr, low, mid - 1, key);

        else

            return BinarySearch(arr, mid + 1, high, key);

    }