-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-binary.c
73 lines (64 loc) · 2.1 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "search_algos.h"
void print_arr(int *array, size_t beg, size_t end);
/**
* binary_search - Searches for a value in a sorted array of integers using the
* Binary search algorithm
* @array: Pointer to the first element of the array to search in
* @size: Number of elements in array
* @value: Value to search for
*
* Description: This function takes a pointer to the first element of a sorted
* array of integers, the size of the array, and a value to search for. It
* uses the Binary Search algorithm to find the value in the array. The
* function divides the array into two halves, checks if the value is in the
* middle, the left half or the right half, and repeats the process until it
* finds the value or exhausts the search space. If the array is NULL, it
* returns -1 immediately.
*
* Return: Index where value is located, or -1 if value is not present or if
* array is NULL
*/
int binary_search(int *array, size_t size, int value)
{
size_t beg = 0;
size_t end = size - 1;
size_t index;
if (array == NULL)
return (-1);
for (;;)
{
print_arr(array, beg, end);
index = ((end - beg) / 2) + beg;
/*printf("index: %li\n", index);*/
if (array[index] == value || beg == end)
break;
else if (array[index] > value)
end = index - 1;
else
beg = index + 1;
}
if (array[index] != value)
index = -1;
return (index);
}
/**
* print_arr - Prints a section of an array of integers
* @array: Pointer to the first element of the array to print from
* @beg: The beginning index of the array section to print
* @end: The ending index of the array section to print
*
* Description: This function takes a pointer to the first element of an array
* of integers, and the beginning and ending indices of the section to print.
* It iterates over the specified section of the array and prints each
* element, separated by commas.
*/
void print_arr(int *array, size_t beg, size_t end)
{
size_t index;
index = beg;
/*printf("beg: %li, end: %li\n", beg, end);*/
printf("Searching in array: %i", array[index++]);
for (; index <= end; index++)
printf(", %i", array[index]);
printf("\n");
}