-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
50 lines (48 loc) · 909 Bytes
/
binarySearch.cpp
File metadata and controls
50 lines (48 loc) · 909 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
#include<iostream>
using namespace std;
int bs(int arr[],int key,int low,int high)
{
int mid;
for(int j=0;j<high;j++)
{
mid=(low+high)/2;
if(arr[mid]==key)
{
return mid;
}
else if(arr[mid]<key)
{
low=mid+1;
}
else{
high=mid-1;
}
}
return false;
}
int main()
{
int arr[20];
int size,low=0,high,key,res;
cout<<"Enter the number of elements : ";
cin>>size;
cout<<"Enter the elements to be searched :";
cin>>key;
cout<<"Enter the elements"<<endl;
high=size-1;
for(int i=0;i<size;i++)
{
cout<<"arr["<<i<<"]";
cin>>arr[i];
}
res=bs(arr,key,low,high);
//cout<<res;
if(res!=-1)
{
cout<<res;
}
else
{
cout<<"not found";
}
}