forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBINARY.CPP
44 lines (43 loc) · 907 Bytes
/
BINARY.CPP
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
/* Implementation of Binary Search */
/* Data Structures Using C by UDIT AGARWAL */
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int arr[20],start,end,mid,n,i,data;
clrscr();
printf("\nHow many elements you want to enter in the array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter element %d:",i+1);
scanf("%d",&arr[i]);
}
printf("\n\nPress any key to continue...");
getch();
do
{
clrscr();
printf("\nEnter the element to be searched:");
scanf("%d",&data);
start=0;
end=n-1;
mid=(start + end)/2;
while(data!=arr[mid] && start <=end)
{
if(data > arr[mid])
start=mid+1;
else
end=mid-1;
mid=(start+end)/2;
}
if(data==arr[mid])
printf("\n%d found at position %d\n",data,mid + 1);
if(start>end)
printf("\n%d not found in array\n",data);
printf("\n\n Press <Y or y> to continue:");
fflush(stdin);
scanf("%c",&ch);
}while(ch == 'Y' || ch == 'y');
}