forked from kishanrajput23/leetcode-solutions-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.c
More file actions
38 lines (38 loc) · 708 Bytes
/
binarySearch.c
File metadata and controls
38 lines (38 loc) · 708 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
#include <stdio.h>
#include <conio.h>
int main()
{
int n, key,l,h,mid,x;
printf("Enter the size of Array");
scanf("%d", &n);
int a[n];
printf("Enter the key");
scanf("%d", &key);
printf("Enter the elements of Array");
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
l = 0;
h = n - 1;
mid = ((l + h)/2);
x=mid;
while (l <= h)
{
if (key == a[x])
{
printf("position of the key in array\n");
printf("%d",x);
break;
}
if (a[x] > key)
{
h = x - 1;
}
if (a[x] < key)
{
l = x + 1;
}
}
return 0;
}