-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
59 lines (59 loc) · 1.39 KB
/
binarySearch.cpp
File metadata and controls
59 lines (59 loc) · 1.39 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
#include<iostream>
using namespace std;
//function return Sorted array
void selectSort(int *a, int n){
int i, j, min, loc;
for(i=0;i<n;i++)
{
min=a[i];
loc=i;
for(j=i;j<n;j++)
{
if(a[j]<min)
{
min=a[j] ;
loc=j;
}
}
int temp;
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
// funct perform binary search and take parameters l-Lower index,u-upper index,s-element to search
int binarySearch(int *a, int l, int u, int s){
int mid;
if(l<=u)
{
mid=(l+u)/2;
//function recursively calls itself passing mid index untill either element is found or lower index > upper index
if(a[mid]==s)
return mid;
if(a[mid]>s)
return binarySearch(a,l, mid-1,s);
if(a[mid]<s)
return binarySearch(a, mid+1,u, s);
}
return -1;
}
int main(){
int n, i, s,c;
cout<<"\nEnter Number of Elements ";
cin>>n;
int a[n];
cout<<"\nEnter "<<n<<" Elements ";
for(i=0;i<n;i++){
cin>>a[i];
}
//calling sort func. to arrange array in sorted order
selectSort(a, n);
cout<<"\nEnter element to search ";
cin>>s;
c=binarySearch(a, 0,n-1,s);
if(c==-1)
cout<<"\nElement not found ";
else
cout<<"\n"<<s<<" found at index "<<c;
return 0;
}