C++---二分查找

#include <iostream>
using namespace std;


//二分查找  2 4 5 9 10    
int binary_search(int arr[], int length, int element)
{
    int left = 0, right = length - 1;

    while (left <= right)
    {
        int middle = (right + left) / 2;

        if (arr[middle] < element)
        {
            left = middle + 1;
        }
        else if (arr[middle] > element)
        {
            right = middle - 1;
        }
        else
        {
            return middle;
        }
    }

    return -1;
};

int main()
{
    int array[] = { 2, 3, 6, 8 ,10, };

    cout << binary_search(array, 5, 10) << endl;
}

posted @ 2020-08-12 14:37  凯旋.Lau  阅读(96)  评论(0编辑  收藏  举报