-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path8.c
More file actions
44 lines (37 loc) · 705 Bytes
/
Copy path8.c
File metadata and controls
44 lines (37 loc) · 705 Bytes
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 <stdio.h>
int binarySearch(int *arr, int low, int high, int item)
{
while (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] == item)
{
return mid;
}
else if (arr[mid] < item)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
int main()
{
int arr[] = {1, 3, 5, 7, 9};
int item = 5;
int n = sizeof(arr) / sizeof(arr[0]);
int index = binarySearch(arr, 0, n - 1, item);
if (index == -1)
{
printf("Item not found.\n");
}
else
{
printf("Item found at index: %d\n", index);
}
return 0;
}