forked from projectX-india/madlab-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary search.c
49 lines (49 loc) · 896 Bytes
/
binary search.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
# include<stdio.h>
int main()
{
int i,j,n,t;
printf("N=");
scanf("%d",&n);
int a[n],c;
for(i=0;i<n;i++)
{
printf("enter the element:");
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("enter the element to search:");
scanf("%d",&c);
int d=0,min=0,max=n-1,mid=(min+max)/2;
while(mid<=max&&mid>=min)
{if(c==a[mid])
{
printf("element found");
d++;
break;
}
else if(c>a[mid])
{
min=mid+1;
mid=(min+max)/2;
}
else
{
max=mid-1;
mid=(min+max)/2;
}}
if(d==0)
{
printf("element not found");
}
}