-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-binary.c
44 lines (37 loc) · 903 Bytes
/
1-binary.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
44
#include "search_algos.h"
/* header */
/**
* binary_search - a function that searches for a value in a
* sorted array of integers using the Binary search algorithm
*
* @array: (int) pointer to aray to be searched, contains integers
*
* @size: (size_t) size of array
*
* @value: (int) value to find in array
*
* Return: index of value if found otherwise return -1
*/
int binary_search(int *array, size_t size, int value)
{
int middle, left, right;
if (array == NULL || size < 2)
return (-1);
left = 0;
right = size - 1;
while (left <= right)
{
printf("Searching in array: ");
for (middle = left; middle < right; middle++)
printf("%d, ", array[middle]);
printf("%d\n", array[middle]);
middle = (left + right) / 2;
if (array[middle] < value)
left = middle + 1;
else if (array[middle] > value)
right = middle - 1;
else
return (middle);
}
return (-1);
}