-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.cpp
More file actions
48 lines (28 loc) · 809 Bytes
/
BinarySearch.cpp
File metadata and controls
48 lines (28 loc) · 809 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
40
41
42
43
44
45
46
47
48
#include<iostream>
using namespace std;
int binarySearch(int arr[] , int size , int key ){
int start = 0;
int end = size - 1;
int mid = start + (end - start)/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 even[6] = {2 , 6 , 11 , 17 , 21 , 26};
int odd[5] = {6 , 12 , 16 , 21 , 35};
int evenIndex = binarySearch(even , 6 , 11);
int oddIndex = binarySearch(odd, 5 , 21);
cout<<"The element 11 is present at index : "<<evenIndex<<endl;
cout<<"The element 21 is present at index : "<<oddIndex<<endl;
}