Skip to content

Commit e520366

Browse files
committed
1-binary
1 parent 3120cbd commit e520366

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
12 KB
Binary file not shown.

0x1E-search_algorithms/1-binary.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "search_algos.h"
2+
3+
/**
4+
* binary_search - searches for a value in a sorted array of integers
5+
* using the binary search algorithm
6+
* @array: array of integers
7+
* @size: size of array
8+
* @value: value to search for
9+
* Return: index of value or -1 if not found
10+
*/
11+
12+
int binary_search(int *array, size_t size, int value)
13+
{
14+
15+
size_t y, left, right;
16+
17+
if (array == NULL)
18+
return (-1);
19+
20+
for (left = 0, right = size - 1; right >= left;)
21+
{
22+
printf("Searching in array: ");
23+
for (y = left; y < right; y++)
24+
printf("%d, ", array[y]);
25+
printf("%d\n", array[i]);
26+
27+
y = left + (right - left) / 2;
28+
if (array[y] == value)
29+
return (y);
30+
if (array[y] > value)
31+
right = y - 1;
32+
else
33+
left = y + 1;
34+
}
35+
36+
return (-1);
37+
}

0 commit comments

Comments
 (0)