forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearch.c
43 lines (32 loc) · 938 Bytes
/
BinarySearch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
int BinarySearch(int array[], int size, int value) {
int start = 0;
int end = size - 1;
int middle = end / 2;
while (start < end && array[middle] != value) {
// new start
if (value > array[middle])
start = middle + 1;
// new end
if (value < array[middle])
end = middle - 1;
// new middle
middle = (start + end) / 2;
}
if (array[middle] == value)
return middle;
return -1;
}
int main() {
int value;
int array[] = {1, 5, 10, 12, 18, 22, 87, 90, 112, 129};
size_t size = sizeof(array) / sizeof(array[0]);
printf("Please provide the number you want to value for: ");
scanf("%d", &value);
int pos = BinarySearch(array, size, value);
if (pos != -1)
printf("Found in position = %d.\nValue = %d\n", pos, array[pos]);
else
printf("Not found\n");
return 0;
}