-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary.cpp
More file actions
54 lines (48 loc) · 756 Bytes
/
Binary.cpp
File metadata and controls
54 lines (48 loc) · 756 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
40
41
42
43
44
45
46
47
48
49
50
51
52
/* binary search */
#include<stdio.h>
void binarysearch(int [],int,int);
int main()
{
int data[100],n,item,i;
printf("enter no of elements ");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&data[i]);
}
printf("enter the no to be searched ");
scanf("%d",&item);
binarysearch(data,n,item);
return(0);
}
void binarysearch(int data[],int n,int item)
{
int end,beg,mid;
beg=0;end=n-1;mid=(end+beg)/2;
while (beg<=end)
{
if(item==data[mid])
{
break;
}
else if(item<data[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=(end+beg)/2;
}
if(beg>end)
{
printf("search unsuccessful\n");
}
else
{
printf("search successful\n");
printf("found at the position %d\n",mid+1);
}
}