forked from swaaz/basicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.c
More file actions
39 lines (35 loc) · 800 Bytes
/
Copy pathprogram.c
File metadata and controls
39 lines (35 loc) · 800 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
#include <stdio.h>
int main() {
int n;//number of elements of array;
printf("Enter the number of elements");
scanf("%d",&n);
int arr[n];
printf("\nEnter the elements");
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
int x;
printf("\nEnter the key to search");
scanf("%d",&x);
int l=0,r=n-1,m=0;
int f=1;
while (l <= r)
{
m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
{
printf("\nElement found");
f=0;
break;
}
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
if(f==1)
printf("\nElement not found");
return 0;
}