aboutsummaryrefslogtreecommitdiff
path: root/binary_search.c
blob: bd8f855e878ddfa0e9fda97af391064536a97119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 704. Binary Search
#include <stdio.h>

int search(int *nums, int nums_size, int target) {
  int low = 0, high = nums_size - 1;
  while (low <= high) {
    int mid = low + (high - low) / 2;
    if (nums[mid] == target)
      return mid;
    else if (nums[mid] > target)
      high = mid - 1;
    else
      low = mid + 1;
  }
  return -1;
}

int main() {
  int nums[] = {-1, 0, 3, 5, 9, 12};
  printf("%d\n", search(nums, 6, 9)); // expect: 4
  printf("%d\n", search(nums, 6, 2)); // expect: -1
}