Skip to content

Commit 04a9c39

Browse files
committed
1-binary.c
1 parent e520366 commit 04a9c39

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

0x1E-search_algorithms/1-binary

16.4 KB
Binary file not shown.

0x1E-search_algorithms/1-binary.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "search_algos.h"
2+
#include <stdio.h>
23

34
/**
45
* binary_search - searches for a value in a sorted array of integers
@@ -11,26 +12,25 @@
1112

1213
int binary_search(int *array, size_t size, int value)
1314
{
14-
15-
size_t y, left, right;
15+
size_t i, left, right;
1616

1717
if (array == NULL)
1818
return (-1);
1919

2020
for (left = 0, right = size - 1; right >= left;)
2121
{
2222
printf("Searching in array: ");
23-
for (y = left; y < right; y++)
24-
printf("%d, ", array[y]);
23+
for (i = left; i < right; i++)
24+
printf("%d, ", array[i]);
2525
printf("%d\n", array[i]);
2626

27-
y = left + (right - left) / 2;
28-
if (array[y] == value)
29-
return (y);
30-
if (array[y] > value)
31-
right = y - 1;
27+
i = left + (right - left) / 2;
28+
if (array[i] == value)
29+
return (i);
30+
if (array[i] > value)
31+
right = i - 1;
3232
else
33-
left = y + 1;
33+
left = i + 1;
3434
}
3535

3636
return (-1);

0 commit comments

Comments
 (0)