forked from shivaylamba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_search.c
43 lines (42 loc) · 798 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
#include<stdio.h> #include<conio.h>
int main()
{
int i, a[10], n, num,mid,low,high;
printf ("enter the number of terms");
scanf ("%d", &n);
printf ("enter the array");
for(i=0;i<n;i++)
{
scanf ("%d", &a[i]);
}
printf ("enter the number to be searched");
scanf ("%d", &num);
low=0;
high=n-1;
mid=(low+high)/2;
while(low<=high)
{
if(a[mid]<num)
{
low=(mid+1);
mid=(low+high)/2;
}
else;
{
if(a[mid]==num)
{
printf("number is found");
break;
}
else;
{
high=mid-1;
mid=(high+low)/2;
}
if(low>high)
{
printf("number is not found");
}
}
}
}