-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.cpp
More file actions
72 lines (67 loc) · 1.34 KB
/
BinarySearch.cpp
File metadata and controls
72 lines (67 loc) · 1.34 KB
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
#include <iostream>
using namespace std;
void BSearch(int *,int ,int );
void Sort(int *, int );
int c;
int main()
{
int a[20];
int val,n;
cout<<"Enter the number of elements in the array: ";
cin>>n;
cout<<"Enter the elements in the array: ";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the element to be searched in the array: ";
cin>>val;
Sort(a,n);
BSearch(a,n,val);
cout<<"c="<<c<<endl;
return 0;
}
void BSearch(int a[],int n,int val)
{
int begin=0,end=n,mid=0;
while(begin<=end )
{
mid=(begin+end)/2;
if(a[mid]==val)
{
c++;
cout<<"Element found at location "<<mid+1<<endl;
return;
}
else if(val<a[mid])
{
end=mid-1;
c++;
}
else
{
begin=mid+1;
c++;
}
}
cout<<"Element is not present in the array\n";
}
void Sort(int a[],int n)
{
int j,t;
for(int i=0;i<n;i++)
{
t=a[i];
j=i-1;
while((t<a[j]) && (j>=0) )
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=t;
}
cout<<"\nSorted list is as follows\n";
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
cout<<"\n";
}