forked from Vatsalparsaniya/Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
46 lines (42 loc) · 1.01 KB
/
Copy pathbinary_search.cpp
File metadata and controls
46 lines (42 loc) · 1.01 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
#include<bits/stdc++.h>
using namespace std;
int binarySearch(int arr[],int low,int high,int elem)
{
int mid;
if(low<=high)
{
mid=low+(high-low)/2;
// cout<<mid<<endl;
if(arr[mid]==elem)
return mid;
else if(arr[mid]>elem)
return binarySearch(arr,low,mid-1,elem);
else
return binarySearch(arr,mid+1,high,elem);
}
return -1;
}
int main()
{
//Provided array is sorted else we will have to use sort function.
int n,elem;
cout<<"Enter the size of the array:\n";
cin>>n;
int arr[n+1];
cout<<"Enter the elements of array:\n";
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"Enter the elements to be searched:\n";
cin>>elem;
// sort(arr,arr+n);
int index=binarySearch(arr,0,n-1,elem);
if(index == -1)
{
cout<<"Element is not present in the given array!\n";
}
else
{
cout<<"Element is present at index "<<index<<" in the given array!\n";
}
return 0;
}