Skip to content

Commit eabf529

Browse files
committed
Binary Search Code using C language
1 parent a881c04 commit eabf529

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

binarySearch.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include <conio.h>
3+
int main()
4+
{
5+
int n, key,l,h,mid,x;
6+
printf("Enter the size of Array");
7+
scanf("%d", &n);
8+
int a[n];
9+
printf("Enter the key");
10+
scanf("%d", &key);
11+
printf("Enter the elements of Array");
12+
for (int i = 0; i < n; i++)
13+
{
14+
scanf("%d", &a[i]);
15+
}
16+
l = 0;
17+
h = n - 1;
18+
mid = ((l + h)/2);
19+
x=mid;
20+
while (l <= h)
21+
{
22+
if (key == a[x])
23+
{
24+
printf("position of the key in array\n");
25+
printf("%d",x);
26+
break;
27+
}
28+
if (a[x] > key)
29+
{
30+
h = x - 1;
31+
}
32+
if (a[x] < key)
33+
{
34+
l = x + 1;
35+
}
36+
}
37+
return 0;
38+
}

0 commit comments

Comments
 (0)