Untitled
raw download clone
C
views 30
,
size 541 b
#ifndef _FUNCTION_H_
#define _FUNCTION_H_

#include <stdio.h>
#include <stdlib.h>


int BS(int*, int, int);

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;
}


#endif
close fullscreen
Login or Register to edit or fork this paste. It's free.