We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a881c04 commit eabf529Copy full SHA for eabf529
binarySearch.c
@@ -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