Untitled
raw download clone
C
views 21
,
size 434 b
#include "function.h"

int BS(int* data,int q,int n)
{
     int low = 0, high = n - 1;

    while (low <= high)
    {
        int mid = (low + high) / 2;

        if (data[mid] == q)
        {
            return mid;
        }
        else if (data[mid] > q)
        {
            high = mid - 1;
        }
        else if (data[mid] < q)
        {
            low = mid + 1;
        }
    }

    return -1;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.