-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.cpp
More file actions
39 lines (39 loc) · 816 Bytes
/
Copy pathbinary.cpp
File metadata and controls
39 lines (39 loc) · 816 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
#include<iostream>
#include"sorting.cpp"
using namespace std;
void binarysearch(int size, int item, int array[])
{
int beg=0,end=size-1,mid=(beg+end)/2;
while(item!=array[mid] && beg<=end)
{
if(item<array[mid])
end=mid-1;
else
beg=mid+1;
mid=(beg+end)/2;
}
if(item==array[mid])
cout<<"\nsearch successful\nitem found at location "<<mid<<endl;
else
cout<<"\nsearch unsuccessful\nitem not found"<<endl;
}
void insert(int size,int array[])
{
cout<<"enter the elements of array"<<endl;
for(int i=0;i<size;i++)
{
cin>>array[i];
}
}
int main()
{
int array[10],size,item;
cout<<"enter the size of array"<<endl;
cin>>size;
insert(size,array);
cout<<"enter the element you want to search within the array"<<endl;
cin>>item;
sorting(array,size);
binarysearch(size,item,array);
return 0;
}