-
Notifications
You must be signed in to change notification settings - Fork 0
/
BS.CPP
98 lines (82 loc) · 1.3 KB
/
BS.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream.h>
#include<conio.h>
class bsearch
{
int array[10];
int low,high,i,j,mid,n;
public:
void input();
void print();
void sort();
void check();
}obj;
void bsearch::input()
{
cout<<"enter no of ele ";
cin>>n;
for(i=1;i<=n;i++)
{
cin>>array[i];
}
}
void bsearch::print()
{
for(i=1;i<=n;i++)
{
cout<<array[i]<<" ";
}
cout<<endl;
}
void bsearch::sort()
{ int temp ;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(array[i]>array[j])
{
temp = array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
}
void bsearch::check()
{
low=1;
high=n;
int num;
cout<<"\nenter num to check ";
cin>>num;
while(low<=high)
{
mid=(low+high)/2;
if(num<array[mid])
{
high=mid-1;
}
else if(num<array[mid])
{
low=mid+1;
}
else
{
if(num==mid)
{ cout<<"\nthe element exists ";
return;}
else
{ cout<<"\nthe element donot exists ";
return; }
}
}
}
void main()
{
clrscr();
obj.input();
obj.sort();
obj.print();
obj.check();
getch();
}