File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments