forked from IOSD/Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary_Search.cpp
46 lines (41 loc) · 1.06 KB
/
Binary_Search.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
//BINARY SEARCH ALGORITHM
#include<iostream>
using namespace std;
int binsearch(int a[],int n,int no) //FUNCTION FOR BINARY SEARCH TAKING ARRAY, SIZE AND NO. TO BE SEARCHED AS ARGUMENTS
{
int pos=-1,beg=0,last=n-1,mid;
while(beg<=last)
{
mid=(beg+last)/2;
if(a[mid]==no)
{
pos=mid;
break;
}
if(a[mid]>no)
last=mid-1;
else
beg=mid+1;
}
return(pos);
}
//TIME COMPLEXITY : O(log n)
//SPACE COMPLEXITY : O(1)
int main()
{
int *b,n,no;
cout<<"Enter NO. of elements:";
cin>>n; //NO. OF ELEMENTS
b=new int[n]; //DYNAMIC INITIALISATION OF ARRAY
for(int i=0;i<n;i++)
cin>>b[i];
cout<<"\nEnter NO. to be searched:";
cin>>no; //NO. TO BE SEARCHED
no=binsearch(b,n,no); //CALLING FUNCTION BINSEARCH
if(no==-1)
cout<<"\nNO. not found.";
else
cout<<"\nNO. found at position:"<<no+1;
delete b;
return 0;
}