-
Notifications
You must be signed in to change notification settings - Fork 1
/
Binary Search
44 lines (38 loc) · 855 Bytes
/
Binary Search
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
#include <iostream>
using namespace std;
int binarySearch(int arr[],int n,int key){
int start=0;
int end=n-1;
int mid=start+(end-start)/2; //int mid=(start+end)/2;
while(start<=end){
if(arr[mid]==key){
return mid;
}
if(key>arr[mid]){
start=mid+1;
}
else{
end=mid-1;
}
mid=start+(end-start)/2;
}
return -1;
}
int main()
{
int arr[7]={2,4,7,9,13,15,19};
for(int i=0;i<7;i++){
cout<<arr[i]<<" ";
}cout<<endl;
int key;
cout<<"Enter the value to find in array: "<<endl;
cin>>key;
int index=binarySearch(arr,7,key);
if(index==-1){
cout<<"Element not found in given array"<<endl;
}
else{
cout<<"Element found at index: "<<index<<endl;
}
return 0;
}