-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.c
More file actions
37 lines (37 loc) · 788 Bytes
/
BinarySearch.c
File metadata and controls
37 lines (37 loc) · 788 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
#include <stdio.h>
int main()
{
int a[11] = {2, 3, 5, 9, 14, 16, 19, 25, 29, 32, 35}, found = 0, ele;
int low = 0;
int high = 10; //n-1
printf("enter the element you want to search:\n");
scanf("%d", &ele);
while (low <= high)
{
int mid = (low + high) / 2;
if (ele < a[mid])
{
high = mid - 1;
}
else if (ele > a[mid])
{
low = mid + 1;
}
else if (ele == a[mid])
{
printf("Found at Index %d\n", mid+1);
found = 1;
break;
}
}
if (found == 0)
{
{
if (!found)
{
printf("Not Found\n");
}
}
}
return 0;
}