-
Notifications
You must be signed in to change notification settings - Fork 11
/
17.c
51 lines (51 loc) · 934 Bytes
/
17.c
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
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
int main()
{
int i,n,num,low,high,mid,a[10],f=0,temp,j;
printf("enter the limit");
scanf("%d",&n);
printf("enter the elemets");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("enter the element to be searched");
scanf("%d",&num);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==num)
{
f=1;
printf("element found at %d",mid+1);
break;
}
else if(a[mid]>num)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
if(f==0)
{
printf("element not found");
}
return 0;
}