6_BinarySearch

//[2 4 5 6 8 10 12 14 17 19]   5

#include

using namespace std;

int  search(int array[], int len, int value)
{
    int low=0,high=len-1;
    while(low<=high)
    {
        //int mid=(low+high)/2;
        int mid=low+(high-low)/2;  //优化代码,防止越界溢出
        
        if(array[mid]value)
            high=mid-1;
        else//低频访问处放在后边
            return mid;
    }
    return -1;
}

int main()
{
    int arr[10]={2,4,5,6,8,10,12,14,17,19};
    cout<

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