summary refs log tree commit diff stats
path: root/c/binary-search/binary_search.c
blob: fe122d4a1fec95f653b3302a1aa922d67b23a230 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "binary_search.h"

const int *binary_search(int value, const int *arr, size_t length) {
    size_t left = 0, right = length;
    while (left < right) {
        size_t mid = left + (right - left) / 2;

        if (arr[mid] < value)
            left = mid + 1;
        else if (arr[mid] > value)
            right = mid;
        else
            return &arr[mid];
    }
    return NULL;
}