-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.cpp
More file actions
39 lines (39 loc) · 882 Bytes
/
Copy pathBinarySearch.cpp
File metadata and controls
39 lines (39 loc) · 882 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>
using namespace std;
int binarySearch(int arr [], int target, int size){
int l = 0; int h = size-1;
while(l<=h){
int mid = l + (h-l)/2;
if(arr[mid]== target){
return mid;
}
else if(arr[mid]>target){
h = mid -1;
}
else{
l= mid+1;
}
}
return -1;
}
double moreprecision(int n, int precision, int tempSol){
int i;
double factor = i;
double ans = tempSol;
for(int i = 0; i<precision; i++){
factor = factor/10;
for(double j=ans;j*j<n; j=j+factor);
}
}
int main(){
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(a)/sizeof(int);
int b = binarySearch(a, 7, n);
if(b==-1){
cout<<"target element is not present";
}
else{
cout<<"element found at index "<<b;
}
return 0;
}